Home » Android » php – FCM push notification issue :- "error":"NotRegistered"

php – FCM push notification issue :- "error":"NotRegistered"

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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 snapshotenter image description here

Answer:

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.

Answer:

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.

Answer:

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

Answer:

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”.

Answer:

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

Answer:

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

Answer:

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.

Answer:

My problem was different then all of the above. The Firebase message that we we’re trying exceeded the maximum size of 4Kb. Very small chance this triggers an “NotRegistered”. But the problem was that I took this log of a moment the app was probably not installed. So also check the size of the Firebase message.