1

I have created and setup Firebase console completely as Google tutorial mentioned. I have implemented Services in my project as well. While I am sending the message from Firebase console it is not receiving in my app. When I am trying to send using the single device then it is showing "Unregistered registration token".

Here is my MyFirebaseInstanceIDService:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        System.out.println("TOKEN::" + refreshedToken);
        //Displaying token on logcat

        SmartApplication.REF_SMART_APPLICATION.writeSharedPreferences("regId", refreshedToken);
//        SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
//        SharedPreferences.Editor editor = pref.edit();
//        editor.putString("regId", refreshedToken);
//        editor.commit();
    }
}

Here is my MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    private static int count = 0;
    String TYPE = "type";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        System.out.println("data getting");
        Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
        Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
        Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());

        //Calling method to generate notification
        //remoteMessage.getNotification().getBody()
        sendNotification(remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody(), remoteMessage.getData());
    }


    //This method is only generating push notification
    //It is same as we did in earlier posts
    private void sendNotification(String messageTitle, String messageBody, Map<String, String> row) {
        PendingIntent contentIntent = null;
        try {
            Intent groupDetailIntent = new Intent(this, UnanimousHomeActivity.class);

            contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100),
                    groupDetailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(contentIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(count, notificationBuilder.build());
        count++;
    }
}

I have stucked since 4 days, please someone help me out as no logical problem found here, some unusual things are happening while integration push.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • Before going and have to send the test push from google console if not then click on link --> https://console.firebase.google.com Then select your project. After selecting your project then click Notification on left side of Navigation drawer List and send the test push link Remember while sending Test Push your device should be in Background or closed.. – yash786 Sep 01 '17 at 11:15
  • Try to use Tools -> Firebase -> Cloud Messaging It can help you to find missing argument – Stanislav Bondar Sep 01 '17 at 11:18
  • These all things I have tried but still not succeed. – Pratik Dasa Sep 01 '17 at 11:29
  • Please review this answer : https://stackoverflow.com/questions/42537072/unregistered-registration-token-in-firebase – Nir Patel Sep 01 '17 at 11:43
  • So `refreshedToken` is received ? – Stanislav Bondar Sep 01 '17 at 11:45
  • I am getting token as well. BUt as per doc token should not refresh everytime untill and unless app data will be cleared or uninstalling an app. @StanislavBondar – Pratik Dasa Sep 01 '17 at 12:08
  • Yes it is. When you sending the message from Firebase console does status of message is changing? – Stanislav Bondar Sep 01 '17 at 12:21
  • Yes it is changing status but showing sent to 0 . @StanislavBondar – Pratik Dasa Sep 03 '17 at 07:40

3 Answers3

0
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e(TAG, "From: " + remoteMessage.getFrom());


        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data: " + remoteMessage.getData().toString());



        }

        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Message data: " + remoteMessage.getData().toString());
            sendnotification(remoteMessage.getNotification().getBody());



        }
    }

    private void sendnotification(String body) {

        Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

        Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Emoji Keyboard")
                .setDefaults(-1)
                .setContentText(body)
                .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                .setSound(notificationsound)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }


}

and after this in manifests add this:

 <!-- Firebase Notifications -->
        <service android:name=".services.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service android:name=".services.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <!-- ./Firebase Notifications -->
Deep Doshi
  • 77
  • 6
0

After you've successfully obtained the token, make sure that you've send it correctly to your server.

Try the recommended action for Invalid Registration Token:

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

For more information, see this documentation.

Teyam
  • 7,686
  • 3
  • 15
  • 22
0

Code is working Fine in my application

Add googleservices-json file in app folder created from firebase and add services in manifest

<service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>