Not Opening Specific Activity on Notification Click When the App Is in Background/Not Running

Specific activity not opening from notification click when the app is not in foreground(app is in recents, not killed!)

You can redirect from the Firebase notification when the app is in Foreground or Background as far as you send in data.

I will provide you an example of how to redirect between activities based on key received from the notification, firebase magically handles the rest.

P.S: If you want to handle the activity that is already open in the background, use taskAffinity to play around.

Handle the Firebase data through FirebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
System.out.println("MESSAGE BODY :" + remoteMessage.toString());
if (remoteMessage.getData().size() > 0) {
//getting the title and the body
String redirect;
String title = remoteMessage.getData().get("message_title");
String body = remoteMessage.getData().get("message_body");
redirect = remoteMessage.getData().get("redirect");
String event_id = remoteMessage.getData().get("event_id");
System.out.println("PUSH MESSAGE = " + remoteMessage.toString());
JSONObject jsonData = new JSONObject(remoteMessage.getData());
System.out.println("RESPONSE :" + jsonData.toString());
if (redirect != null) {
sendNotification(title, body, event_id, redirect);
} else {
redirect = "";
sendNotification(title, body, event_id, redirect);

}
}
}


private void sendNotification(String title, String body, String event_id, String redirect) {
Intent backIntent = new Intent();
PendingIntent pendingIntent;
if (redirect.contentEquals("CHAT")) {
backIntent = new Intent(MyFirebaseMessagingService.this, ChatScreen.class);
backIntent.putExtra("item_id", event_id);
}
if (redirect.contentEquals("EVENT") || redirect.contentEquals("INVITATION")) {
backIntent = new Intent(MyFirebaseMessagingService.this, Event_Summary.class);
backIntent.putExtra("event_id", event_id);
}
backIntent.putExtra("MODE", "FIRE_NOTIFICATION");
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(backIntent);
pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

Not opening specific activity on notification click when the app is in background/not running

As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity.
For More information Check this link

Firebase push notification activity is not opening when app is in background?

If you are using firebase push notification

You need to send a click_action attribute ...to open the desired activity when app is in background.

So try ..

 Intent i = new Intent(click_action);

click_action is the intent-filter mapping to the desired activity. click_action is mandatory

instead of the Explicit intent call - explicit intent call in the above case will only work when the app is in foreground..

Push notification need to open a specific activity

If notification clicks on app background state, then that will open app launcher activity. In launcher activity you need to check if there is any pending intent from notification and execute that.

Try this in your launcher activity(MainActivity).

Intent intent=new Intent();
Intent fromIntent = getIntent();
if (fromIntent.getExtras()!=null){
try {
Map<String,String> opendata = remoteMessage.getData();
String actionValue = opendata.get("openactivity");

switch (actionValue){

case "Activity1":
intent=new Intent(this, Activity1.class);
break;
case "Activity2":
intent=new Intent(this, Activity2.class);
break;
default:
intent = new Intent(MainActivity.this, DefaultActivity.class);
break

}
} catch(Exception e){
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
} else{
intent = new Intent(MainActivity.this, DefaultActivity.class);
}
startActivity(intent);

Open Activity on notification button click when app is closed

You can try to receive the click in a BroadcastReceiver and then open activity from there.

  1. Try this to add a action button o your notification:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");

Where the createNotificationActionButton method is this:

public NotificationCompat.Action createNotificationActionButton(String text){
Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);

@SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);

return new NotificationCompat.Action(0, text, pendingIntent);
}

  1. Create a class named StopwatchNotificationActionReceiver and make it extent a BroadcastReceiver`. This is the code for that class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class StopwatchNotificationActionReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
PrefUtil.setIsRunningInBackground(context, false);
PrefUtil.setTimerSecondsPassed(context, 0);
PrefUtil.setWasTimerRunning(context, false);
context.stopService(MainActivity.serviceIntent);
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
}
}

Also you need to register that receiver in your manifest like this:

<receiver android:name="StopwatchNotificationActionReceiver"/>

  1. Where the MainActivity.serviceIntent is a public static variable which looks like this:
public static Intent serviceIntent;

And this intent is only used to start the service like this:

//In onCreate
serviceIntent = new Intent(this, TimerService.class);

//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
}







Or you can try the simple method:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
Context context = this;
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
killService();
}

Edit


Another solution is here. Again. You need to refer to my repo as I have made changes to the files in order to complete your task. In the service class, refer to this method. There, I start the activity if the action is reset(r). Or else, it opens the broadcast receiver. Then, in the activity, I receive that extra in the onResume() method. If the reset button is not clicked, it opens the Receiver class.

And as always, you can view the result of the app from here.

I hope that code will do your work.



Related Topics



Leave a reply



Submit