Arbitur
Arbitur

Reputation: 39081

Android Notification open currently running app instead of creating a new process?

Im receiving a push notification with a custom Uri scheme: myapp://main

In my onReceive i create a notification:

public void createNotification(Context context, String title, String message, String summary, Uri uri) {
    Notification.Builder notification = new Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_launcher)
            .setDefaults(Notification.DEFAULT_LIGHTS)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && summary != null)
        notification.setSubText(summary);


    Intent intent = new Intent("android.intent.action.VIEW", uri);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    notification.setContentIntent(pendingIntent);

    Notification noti;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        noti = notification.build();
    else
        noti = notification.getNotification();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, noti);
}

When I then tap on that notification it opens a new Activity which is my MainActivity. But it seems like it creates a whole new process aswell instead of just opening my currently running app.

Are there some flags Im missing?

Upvotes: 0

Views: 1045

Answers (2)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

There are LaunchModes for Activity

android:launchMode=["multiple" | "singleTop" |"singleTask" | "singleInstance"]

Read more about LaunchMode here

This can be placed in your Activity tag in manifest.

To get the intent data try onNewIntent()

Upvotes: 1

ruike
ruike

Reputation: 11

You can set Intent as follow,the Intent action and category match with you specify in AndroidManifest.xml

 Intent intent = new Intent()  
            .setAction(Intent.ACTION_VIEW)  
            .addCategory(Intent.CATEGORY_DEFAULT)  
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP) 
            .setPackage(getPackageName())
            .setData(uri);

Upvotes: 1

Related Questions