当前位置: 首页 > 知识库问答 >
问题:

FCM消息推送问题:"错误":"未注册"

顾英发
2023-03-14

我遇到了一个奇怪的问题,使用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);
    }

所以问题是当我发送单个通知时,它可以正常工作,但当我发送多个通知时,我每次都出错

<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"

共有3个答案

葛深
2023-03-14

这是客户端(设备)问题,而不是服务端问题。多个场景可能会导致这种情况:

  • 如果客户端应用程序在GCM注销

看https://developers.google.com/cloud-messaging/http-server-ref

在应用程序启动时,我检查我在本地存储的令牌是否与新令牌匹配。如果没有,则刷新服务器上的令牌。我也在FirebaseInstancedService::onTokenRefresh中这样做。

轩辕煌
2023-03-14

根据文档,这是因为移动设备测试不再安装你的应用程序

如果未注册,则应从服务器数据库中删除注册ID,因为应用程序已从设备中卸载,或者客户端应用程序未配置为接收消息

段志
2023-03-14

我对php了解不多,但最近我在另一个项目中遇到了同样的问题,我通过这种方式解决了这个问题:

首先请参考:我在哪里可以找到Firebase云消息传递的API密钥?

 类似资料:
  • 我正在尝试使用Google PHP API客户端https://github.com/googleapis/google-api-php-client/ 通过Google HTTPv1 API发送FCM推送通知。 以下是实际答复: 我一直得到的反应基本上是禁止的。(我也可以在谷歌开发者控制台中看到) Firebase Cloud Messaging API与Google Cloud Messagi

  • 消息推送 PDF版下载 如流开放了消息发送接口,企业可以使用这些接口让企业应用与用户间进行双向通信。 推送消息 向成员推送消息 请求方式:POST(HTTPS) 请求地址:https://api.im.baidu.com/api/message/send?access_token=ACCESS_TOKEN 请求body:(每种类型的消息请求body不同,详见消息推送格式) 参数说明: 参数 类型

  • 1、离线消息 接口说明: 接口类型:回调型接口 接口作用:智齿将客服发送给用户的离线消息推送至企业预先配置好的回调地址上。 请求方法: POST 请求格式: { "type": 202, //消息类型,表示客服发送消息给客户 "partnerId": "", //企业自己的用户id "msgId": "" ,//消息id "content": "" ,//客

  • 这个问题是由一个打字错误或一个无法重现的问题引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能对未来的读者有所帮助。 网址: 标题: 正文: 但是,如果我使用Firebase Notification composer在我的Android设备上使用相同的FCM令牌发送通知,我会收到通知。我在邮递员这里做错了什么?我已经在SO上尝试了几个与此相关的答案,但没有得到任何有用的答案。

  • 1、可以用定时器定时推送数据 require_once __DIR__ . '/Workerman/Autoloader.php'; use WorkermanWorker; use WorkermanLibTimer; $worker = new Worker('websocket://0.0.0.0:1234'); // 进程启动后定时推送数据给客户端 $worker->onWorkerSta

  • 使用Phonegap推送通知,如下所示 http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phoneGap-application/ 在使用.apk构建.apk时,我得到了这个错误.. 失败 失败:生成失败,出现异常。 出了什么问题: null 总时间:15.085秒 错误:带有args:/s,/