I am using firebase push notification and also my android device get notification too. if I get notification, while app is open and I can read "title" and "Message" and it take me to "UserDetailsActivity" a specific class .But I also want to do the same thin on click the the notification. But when click on the notification it doesn't open the "UserDetailsActivity" and it open launchering clas and i can't read the message . Any one have solution for it?
firebaseService class
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { private static int count = 0; @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); Log.d(TAG, "From: " remoteMessage.getFrom());
// Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " remoteMessage.getData()); startService(new Intent(getApplicationContext(),UserDetailsActivity.class));
/* Message data payload: Message Notification Body: */
} // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " remoteMessage.getNotification().getBody()); } }
}
*androidMainfest
<service
android:name=".backgroundService.FirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
CodePudding user response:
As per the documentation, when messages with both notification and data payload are received and application is in background mode, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of launcher Activity. Option 1: You can try to to handle the data in launch activity and from there launch UserDetailsActivity and finish launcher activity quickly without showing UI. https://firebase.google.com/docs/cloud-messaging/android/receive
Option 2: Dont send notification part from FCM and send only payload part. In onmessagereceived() method ,construct the notification with pending intent for UserDetails activity or service.
CodePudding user response:
On launcher activer
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " key " Value: " value);
}
}