0

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?

Arbitur
  • 38,684
  • 22
  • 91
  • 128

2 Answers2

1

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);
ruike
  • 11
  • 4
  • This works! Now Im just wondering how I can get the parameters from the Uri? Because onCreate wont get called anymore. – Arbitur Oct 30 '15 at 12:03
  • If this activity instance is exist, The android system call onNewIntent(), Otherwise it call onCreate() – ruike Nov 02 '15 at 02:18
1

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()

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83