FCM push notification issue: "error":"NotRegistered"

69,381

Solution 1

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.

Solution 2

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

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

Solution 4

The thing is firebase generates a unique device-ID for your target device when the app is run for the first time, and it will be 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 reinstalling or reopening the app the device-ID will differ. This will result in the ID not be identified by firebase to send the notification. This will result in the error Not Registered

Solution 5

I got this error when i uninstalled and reinstalled my application.

What i think is, when we reinstall application, we cant get a new fcm token every time we install.

So, we must first delete the previous instance id and then create new fcm token. Please see the code below..

Just adding the uncommented line resolved my issue..

See first comment for this solution for code :)

_firebaseRegister() { 
    // _firebaseMessaging.deleteInstanceID(); 
    _firebaseMessaging.getToken().then((token) => fcmtoken = token); 
}

Hope this works for you! :)

Share:
69,381
Shashank Shah
Author by

Shashank Shah

I've been working as a PHP developer for 3+ years. Connect me via [email protected]

Updated on July 05, 2022

Comments

  • Shashank Shah
    Shashank Shah almost 2 years

    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 get

    "error":"NotRegistered"