作者:凹凸曼00威威_694 | 来源:互联网 | 2023-02-11 19:23
我正在使用FCM向Android发送推送通知这一奇怪的问题.
目标: - 发送推送通知时出错
下面是我有功能向Android发送推送通知的场景
public static function SendMultipleNotificationAndroid($groups)
{
//your api key SERVER API KEY
$apiKey = Yii::$app->params['android_api_key'];
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization:key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
foreach($groups as $resG){
$users = $resG['users'];
$msg = $resG['message'];
$type = $resG['notification_type'];
$notification_data = $resG['notification_data'];
$deviceTokens = [];
foreach($users as $resUser){
$deviceTokens[] = $resUser['device_token'];
//Add Friend badge count +1
Common::AddRemoveBadgeCount($resUser['user_id']);
}
if(!empty($deviceTokens)){
$fields = array(
'registration_ids' => $deviceTokens,
'priority' => 'high',
'collapse_key' => $resG['notification_type'],
'time_to_live' => 2419200,
"click_action" =>"NotificationListingActivity",
'data' => [
"title" => "ProjectName",
"body" => $resG['message'],
"action_tag" => $resG['notification_type'],
"message" => $resG['message'],
'notification_type' => $type,
'notification_data' => $notification_data,
'sound' => 'default',
]
);
//Print result
p($ch,0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($ch);
}
}
curl_close($ch);
}
所以问题是当我发送单个通知它工作正常,但当我发送多个通知我每次都有错误
Resource id #5
{"multicast_id":4818908994630396118,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136045570022%c3bae3c6002e9358"}]}
Resource id #5
{"multicast_id":5218359780835228544,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136046618669%c3bae3c6002e9358"}]}
当我们调试代码时,我们在数据库中没有设备令牌,没有防火墙停止发送推送通知.
每次我打电话给我以上的功能
"错误": "NotRegistered"
任何帮助,将不胜感激.
提前致谢.
1> Maddy..:
不太了解PHP,但最近我在另一个项目中遇到了同样的问题,我已经解决了这个问题:
首先参考:
在哪里可以找到Firebase Cloud Messaging的API KEY?
并获取更新的API密钥,如下面的快照所示
2> 小智..:
根据该文档,因为移动设备测试不再安装您的应用程序
如果是NotRegistered,则应从服务器数据库中删除注册ID,因为应用程序已从设备卸载,或者客户端应用程序未配置为接收消息.