Khaled Alhayek
Khaled Alhayek

Reputation: 583

can not read extra from intent when app startup from notification

I am using FCM to receive Messages and on onMessageReceived I have the code

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Service.NOTIFICATION_SERVICE);
            Intent notifyIntent = new Intent(this,HelloBubblesActivity.class);
            notifyIntent.putExtra("id",sender);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);


            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setContentTitle("New Message from " + name)
                            .setSmallIcon(R.drawable.smalllogo)
                            .setAutoCancel(false)
                            .setContentText(text)
                            .setContentIntent(pendingIntent);
            notificationManager.notify(sender, mBuilder.build());

When the app is opening from the notification I have the problem which is getting extra is -1 as the code here

int chatId = getIntent().getIntExtra("id",-1); 

Did I miss some thing ?

Upvotes: 2

Views: 1300

Answers (2)

Khaled Alhayek
Khaled Alhayek

Reputation: 583

As FCM doc

To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived callback, which is provided for most message types, with the following exceptions:

Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default. Messages with both notification and data payload. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

so onMessageReceived will never trigger if the application in Background and when you click on the notification the MainActivity will launch, all you need is to get the intent and get the Extra as its sent from server

if (null != getIntent().getExtras() )
        {
            chatId = getIntent().getStringExtra("sender");
        }

Upvotes: 2

Toukea Tatsi
Toukea Tatsi

Reputation: 199

Insure you that your Activity is not singleInstance enable. some time was this problème with singleInstance Activity

Upvotes: 0

Related Questions