我收到了奇怪的问题,发送推送通知给Android使用FCM。
目标:-发送推送通知时出错
下面是我向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);
}
所以问题是,当我发送单个通知时,它可以正常工作,但是当我发送多个通知时,每次都会出错。
<pre>Resource id #5</pre>{"multicast_id":4818908994630396118,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136045570022%c3bae3c6002e9358"}]}
<pre>Resource id #5</pre>{"multicast_id":5218359780835228544,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136046618669%c3bae3c6002e9358"}]}
在调试代码时,数据库中确实有设备令牌,没有防火墙,它停止发送推送通知。
每次调用上述函数时,我都会得到
"error":"NotRegistered"
发布于 2018-04-23 15:53:37
根据文档,it是因为移动设备测试不再安装应用程序。
如果是NotRegistered,则应该从服务器数据库中删除注册ID,因为应用程序已从设备卸载,或者客户端应用程序未配置为接收消息。
发布于 2017-02-21 12:01:46
我对php不太了解,但最近我在另一个项目中遇到了同样的问题,我已经以这样的方式解决了这个问题:
第一页:Where can I find the API KEY for Firebase Cloud Messaging?
并获得更新的API密钥,如下面的快照所示
发布于 2018-08-18 13:50:24
这是一个客户端(设备)问题,而不是服务端。多个方案可能导致这种情况:
请参阅https://developers.google.com/cloud-messaging/http-server-ref
在应用程序启动时,我检查本地存储的令牌是否与新令牌匹配。如果没有,则在服务器上刷新令牌。我也在FirebaseInstanceIDService::onTokenRefresh
中这样做。
https://stackoverflow.com/questions/42241432
复制相似问题