Notification Click: Activity Already Open

Notification click: activity already open

You need to set the launchMode attribute of the Activity you are starting to singleTop. This will cause incoming Intents to be delivered to the existing instance rather than starting a new instance when that Activity is already at the top of the task's stack.

This is done in the manifest by adding android:launchMode="singleTop" to the <activity> element. To access the latest Intent (if you are interested in any data that may have passed in with it), override onNewIntent() in your Activity.

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.

Notification: Open different Activities dynamically on Android 12 - trampoline restrictions issue

Answering my own question.

I ended up choosing Solution #2, which is the hacky solution:


  1. Use a transparent dummy Activity as the destination. Have the TransparentDeeplinkHandleActivity::class.java to handle which Activity to open.

The reason is that SDK needs to send the analytics before opening the client's "real" Activity.

Pre-Android 12, the SDK is architected to open the BroadCastRecevier and it sends analytics. And then the BroadCastReceiver opens the Activity.

The BroadCastRecevier is now replaced with TransparentDeeplinkHandleActivity for TargetSDK >= 31



Related Topics



Leave a reply



Submit