PHP gcm推送通知到多个设备错误

Posted

技术标签:

【中文标题】PHP gcm推送通知到多个设备错误【英文标题】:PHP gcm push notification to multiple device error 【发布时间】:2016-12-04 19:39:27 【问题描述】:
<?php
require 'connecting.php';

$title="SiQi:";
$message="Are you safe?";
sendPush($title,$message);

function sendPush($registrationIds,$title,$message)

$result = mysql_query("SELECT regids FROM user");  
while($row = mysql_fetch_assoc($result))
$json[] = $row;


echo json_encode($json);
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'AIzaSyAToG8Xw3g6PbpM55-ZmTckLebGsYUGLns');
$registrationIds = $json;
print_r($registrationIds);
$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1

// you can also add images, additional Data
);

$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
 );
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
); 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

?>

以上是我的 php 代码。当我运行我的 php 时,我得到了这个结果。它显示了我数据库中的 2 个令牌,但它不起作用。

我参考了许多其他帖子,但仍然无法解决这个问题。谁能告诉我如何更改我的代码以使其正常工作?

["regids":"eKZu8R9f3lQ:APA91bH8hL8Mi1-CizwW3Gir2tQ-Ojx6GoBmYPmnPtZv6z_jf_HO27KJQX1YQSHV6Z7Av8ievbtU8VOgtJVMVj0D5AeRmRuUmNhSYKQ-cU4mbbJt-_2E0SqT4xda3WcCumCYKd6uxqIy","regids":"c5JsjAL-gQ4:APA91bF0XmzjD7D2UpcHpNpsUU-kw911yCymga792n19ZtI5QfA3schZQ3Do9ZO5UUwktyCa3SMNmcdH5G1EBsERZ-6vwFNWzXLB7Y0MxQvA54slfXm-QgNjWL-RftlYYXQvdIhmTjtx"]Array ( [0] => Array ( [regids] => eKZu8R9f3lQ:APA91bH8hL8Mi1-CizwW3Gir2tQ-Ojx6GoBmYPmnPtZv6z_jf_HO27KJQX1YQSHV6Z7Av8ievbtU8VOgtJVMVj0D5AeRmRuUmNhSYKQ-cU4mbbJt-_2E0SqT4xda3WcCumCYKd6uxqIy ) [1] => Array ( [regids] => c5JsjAL-gQ4:APA91bF0XmzjD7D2UpcHpNpsUU-kw911yCymga792n19ZtI5QfA3schZQ3Do9ZO5UUwktyCa3SMNmcdH5G1EBsERZ-6vwFNWzXLB7Y0MxQvA54slfXm-QgNjWL-RftlYYXQvdIhmTjtx ) ) "multicast_id":7329308833098908144,"success":0,"failure":2,"canonical_ids":0,"results":["error":"InvalidRegistration","error":"InvalidRegistration"]

【问题讨论】:

【参考方案1】:

将您的$registrationIds 放入数组中,例如array('eKZu8R9f3lQ','eKZu8R9f3lQ')

如果上述解决方案未修复,请从设备获取更新的 GCM 令牌。

见:Using GCM to send notifications on app, returns InvalidRegistration error

【讨论】:

您好,感谢您的评论!我已经通过从我的数据库中调用 regids 解决了这个问题

以上是关于PHP gcm推送通知到多个设备错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 GCM 和 java 向多个设备发送推送通知

使用此代码向多个设备发送推送通知 (PHP)

使用 GCM、PHP 和 MySQL 的 Android 推送通知

React本机推送通知,GCM服务器返回401

在多个设备上发送推送通知

某些 android 设备未收到 GCM 推送通知