【问题标题】:array_key_exists() expects parameter 2 to be array, string when using pushWoosh Classarray_key_exists() 期望参数 2 是数组,使用 pushWoosh 类时的字符串
【发布时间】:2014-09-07 10:15:53
【问题描述】:

我在实现 pushwoosh 类 http://astutech.github.io/PushWooshPHPLibrary/index.html 时遇到了一些问题。我已经设置好了所有东西,但是我收到了类中的数组错误。

这是我提供给班级的代码:

<?php
require '../core/init.php';

//get values from the clientside
$sendID = $_POST['sendID'];
$memID = $_POST['memID'];

//get sender name
$qry = $users->userdata($memID);
$sendName = $qry['name'];

//get receiving token
$qry2 = $users->getTokenForPush($sendID);
$deviceToken = $qry2['token'];
//i have testet that $deviceToken actually returns the $deviceToken so thats not the problem

//this is the array that the php class requires.
$pushArray = array(
        'content'   => 'New message from ' . $sendName,
        'devices'   => $deviceToken,
);

$push->createMessage($pushArray, 'now', null);
?>

这是 createMessage() 方法的实际代码

public function createMessage(array $pushes, $sendDate = 'now', $link = null, 

$ios_badges = 1)
{
// Get the config settings
$config = $this->config;
// Store the message data
$data = array(
'application' => $config['application'],
'username' => $config['username'],
'password' => $config['password']
);
// Loop through each push and add them to the notifications array
foreach ($pushes as $push) {
$pushData = array(
'send_date' => $sendDate,
'content' => $push['content'],
'ios_badges' => $ios_badges
);
// If a list of devices is specified, add that to the push data
if (array_key_exists('devices', $push)) {
$pushData['devices'] = $push['devices'];
}
// If a link is specified, add that to the push data
if ($link) {
$pushData['link'] = $link;
}
$data['notifications'][] = $pushData;
}
// Send the message
$response = $this->pwCall('createMessage', $data);
// Return a value
return $response;
}
}

有没有聪明的头脑可以告诉我出了什么问题?

【问题讨论】:

    标签: php arrays class


    【解决方案1】:

    如果我理解,如果您当前正在阅读 devices 子数组,您正在尝试这样做。你应该试试这个:

    foreach ($pushes as $key => $push) {
        ...
        // If a list of devices is specified, add that to the push data
        if ($key == 'devices') {
            $pushData['devices'] = $push['devices'];
        }
    

    您遍历$pushes,即array('content' =&gt; ..., 'devices' =&gt; ...)。您将首先拥有$key = 内容,$key = 'devices'。

    【讨论】:

    • 嗯,如果我在这里理解你的话,我不舒尔。您的意思是更改 pushWosh 类中的代码吗?我还没有上课,只是想实现它。可以在此处找到该类的文档。 astutech.github.io/PushWooshPHPLibrary/…
    【解决方案2】:

    看起来createMessage 函数需要一组消息,但您直接传入一个消息。试试这个:

    $push->createMessage(array($pushArray), 'now', null);
    

    【讨论】:

    • 只要你总是只发送一个,这将起作用,这使得 foreach 没有用,但如果这是需要的,这就是类所要求的就是这样。
    • 感谢 Wolfgang 的提示,这很有效!我仍然没有在我的设备上收到任何推送通知,但是错误数组消息消失了:)非常感谢。
    【解决方案3】:

    该类需要一个数组数组;你只是提供一个数组。

    你可以这样做

    //this is the array that the php class requires.
    $pushArrayData = array(
            'content'   => 'New message from ' . $sendName,
            'devices'   => $deviceToken,
    );
    $pushArray[] = $pushArrayData
    

    您是否想要处理多条消息?这会影响我的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-28
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多