FCM push notification issue :- “error”:“NotRegistered”

故事扮演 提交于 2019-12-07 01:05:58

问题


I am getting weird issue of sending push notification to android using FCM.

Goal :- Having error while sending push notification

Below is the scenario I do have function for sending push notification to 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);
    }

So the issue is when i send single notification it works fine but when i send multiple notification i got error every time

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

As we debug the code we do have device token in our database no firewall which stops sending push notifications.

Every time i call above function i got

"error":"NotRegistered"

Any Help Would be appreciated.

Thanks in advance.


回答1:


Don't know much about php, but recently I have faced the same issue in another project and I have resolved this way :

Refere this first : Where can I find the API KEY for Firebase Cloud Messaging?

and get updated API key as shown in below snapshot




回答2:


According to the doc its because the mobile device testing does not have your app installed anymore

If it is NotRegistered, you should remove the registration ID from your server database because the application was uninstalled from the device, or the client app isn't configured to receive messages.




回答3:


This is a client-side (device) issue, not service-side. Multiple scenarios can cause this:

  • If the client app unregisters with GCM.
  • If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
  • If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired for iOS devices).
  • If the client app is updated but the new version is not configured to receive messages.

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

On app startup I check to see if the token I have stored locally matches the new token. If not then I refresh the token on my servers. I also do this in FirebaseInstanceIDService::onTokenRefresh.




回答4:


See if you have uninstalled the app and your device token is modified. Update the device token, and your error will be gone




回答5:


While searching for the "NotRegistered" issue, we found the following ...

At device end, the generation of device notification token was done by following code once, when user first time starts the app after installation.

RegToken = FirebaseInstanceId.getInstance().getToken(senderId, "FCM");  // Old code

We were using other class derived from "FirebaseMessagingService" to create / receive notifications. But the following method was missing in that class.

// New code
@Override
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(token);
 }

We found that, the above method was called by FCM system in device on every start of App. ( The App was not Uninstalled, still the method was giving different token every time. ) So we called our method 'sendRegistrationToServer(token);' to submit the token to our server along with the 'DeviceId' and other identification data. When we sent notification from php server on this token, it returned 'sent' instead of "NotRegistered".




回答6:


I have a 100 percent solution i had fix recently this issue this error, occurring because you are sending this notification on a device which does does not contain your firebase setup api key For example when you registered user that time user registered from different firebase setup so your android token was different and your sending request to other firebase setup for which didn't create android token whom you trying to send notification(message) so you would have to make sure your user android token generating from same firebase project of which project you are using firebase api key




回答7:


The thing is firebase generates a unique device Id for your target device when the app is run for the first time,and is being used as the identity of the device,If the User uninstalls the app or clears the data of the app then In that case On re installing or reopening the app the device Id will differ which will not be identified by firebase to send the notification this will result in the error Not Registered




回答8:


This error appeared in an Android application when sending request to https://fcm.googleapis.com/fcm/send:

...
"results": [
    {
        "error": "NotRegistered"
    }
]

While the application was working well for some time, and push notifications were delivered, then (probably after reauthorization in the app or reauthorization in Play Market) that error started to show.

If I changed push token (added "1", for instance), then I got another error:

"results": [
    {
        "error": "InvalidRegistration"
    }
]

So, Firebase knew about the push token, but didn't deliver a notification.

I uninstalled the Android application and installed again.



来源:https://stackoverflow.com/questions/42241432/fcm-push-notification-issue-errornotregistered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!