3

I have integrated the FCM in my app, I can receive the notification while the App is running or killed etc. but If the app is running then I can be able to navigate the specific screens. but If the app killed or closed, then If I clicked the notification then always it's redirect to the home screen not for the navigated area. this is the code I used :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

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


    //  if (remoteMessage.getNotification() != null) {
    String msg = remoteMessage.getNotification().getBody();

    if (!TextUtils.isEmpty(msg)) {

        sendNotification(remoteMessage.getData(), msg);
    }
}
 private void sendNotification(Map<String, String> data, String messageBody) {

  String referenceKey = data.get("ReferenceKey");
    String referenceValue = data.get("ReferenceValue");

 switch (referenceKey) {
                case Repository.ModuleCode.BRAND:
                        intent = new Intent(this, WebViewActivity.class);
                        intent.putExtra("ID", referenceValue);
                        intent.putExtra("browser", false);
                    break;

                case Repository.ModuleCode.NEWS:
                        intent = new Intent(this, NewDetailActivity.class);

                    break;

                    }
                     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
                    }

in the manifest :

 <service
        android:name=".fcm.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">

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

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

in App Gradle

  compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.google.android.gms:play-services-maps:9.6.1'

I cannot navigate the exact page if the app killed or closed cases only. Thanks in advance

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
AngelJanniee
  • 613
  • 1
  • 11
  • 30

3 Answers3

3

Finally I changed the Payload from the backend then it solved my problem.

Previously I used the PayLoad like this

{ "notification": {
"text": "Your Text1"
},
"data": {
"ReferenceKey": "PR" ,
"ReferenceValue": "10," 
},
"to" : "pushtoken"
} 

then I remove the notification and used like this.

 {
"data": {
"Message": "Test",
"ReferenceKey": "PR" ,
"ReferenceValue": "10," 
},
"to" : "pushtoken"
}

Then it's working for foreground / Killed / closed.

After this I cannot get notification in the xiaomi note3 only.

AngelJanniee
  • 613
  • 1
  • 11
  • 30
  • Enable auto start permission on by manually or from program check this link https://stackoverflow.com/questions/39368251/how-to-enable-autostart-option-for-my-app-in-xiaomi-phone-security-app-programma. – Sandeep Jan 23 '18 at 10:19
  • Can we able to track this notifications and do analytics using this method ? – Nizamudeen Sherif Apr 30 '19 at 19:18
2

It's working as expected ..onMessageReceived will not get triggered if the app is killed or in background.If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.

You can use getIntent().getExtras(); for fetching the intent while launching to get the intent.

more info here;

eg:

      Bundle bundle = getIntent().getExtras();
      if (bundle != null) {
          if (bundle.containsKey("data")) {
          Intent intent = new Intent(mContext, ExpectedActivity.Class)
          intent.putExtras("PUSH_KEY",bundle.get("data").toString());
          startActivity(intent)
        }
      }

Place this code in your launcher activity.And this will navigate you to your expected activity even when the app is killed or is in background.

Or

You can call your customized activity on click of notification if your app is in background by calling rest service api for firebase messaging as given here https://stackoverflow.com/a/37599088/3111083.

Community
  • 1
  • 1
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
  • I have done the above code and it's working foe me too... I can get the notification as expected. but my case, If my app closed or killed. then I can get the notification. but If I click that notification that is not redirect to expected page. but If the app is running then it will navigated to expected page. – AngelJanniee Dec 04 '16 at 04:56
  • @AngelJanniee And the reason for not redirecting to expected page is because the notification data is not recieved in "onMessageReceived" .You are redirecting to your expected page from "onMessageReceived". You have do the same from your launcher page as well.. – Sunil Sunny Dec 06 '16 at 05:17
  • Okay. I will do it and test that, but If app running then I can get the notification in the notification bar. but If I click any notification then it's redirect to the latest received items only. then If I click other notification icon then it will not call anything Can you suggest for this issue?? – AngelJanniee Dec 06 '16 at 05:21
  • @AngelJanniee Try my eg : code in launcher activity. – Sunil Sunny Dec 06 '16 at 05:36
  • I have added the code also I logged the details it's showing the previous ID and the KEY if I click the latest notification. then If I click the other notification icons it will not happening. – AngelJanniee Dec 06 '16 at 05:59
0

onMessageReceived only works when your application is in the foreground, if the application is in the background or killed it wont work, the notification will inflate with the title and message body. If you click on the notification it will direct you to apps launcher activity and the notification's data object can be retrieved from the getIntent in the onCreate.

The data's object key/value has already been serilies into the intent, so just extract your data with the key/varible from your data object.

For example:

Bundle extras  = getIntent().getExtras();
String msg = extras.getString("time");

Note you can only get the data if the notification is clicked on.

Pang
  • 9,564
  • 146
  • 81
  • 122
ssk360
  • 11
  • 1