How to Automatically Open App When Receive Push Notification

How to automatically open app when receive push notification?

This is something need to handle from backend,

Here is a sample payload you are using right now,
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}

Which will only give you control to manipulate and do some action when your app will be in foreground otherwise just raise notification.

In details you can check here.

Now, To always get control over your notification, you need payload like following,

{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}

The difference is you need to send data payload instead of notification poayload from backend.

How to auto launch android app when you receive notification [FCM]?

In your onMessageReceived() method, you can try adding your startActivity(intent) code. That way, when the app receives a FCM message, it launches the app. Like this...

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
startActivity(new Intent(this, MainActivity.class));
}
}

How to automatically Open the app without user action on receiving a push on Android

PackageManager.getLaunchIntentForPackage can help you find the entry activity.

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);


Related Topics



Leave a reply



Submit