0

I'm using OneSignal for my Android notifications. I am able to receive the notification and start an activity in my own NotificationOpenedHandler():

String code = payload.additionalData.getString(Cons.KEY_CODE);
if(code.equalsIgnoreCase(Cons.NOTIFICATION_DETAIL_SCREEN)) {
    Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
    startActivity(intent);
}

The problem is that if I'm in another app and I click the notification, my app will not be brought to the front. Although when I navigate to my app, the activity I specified above has launched as intended.

I've disabled the default launcher in my manifest as well:

<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />

Anyone know why my app is not being brought to front?

jwBurnside
  • 849
  • 4
  • 31
  • 66
  • try setting appropriate flags with intent. I am using intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); and it works for me – Gautam Mar 27 '17 at 18:10
  • Doesn't seem to work in my case. Notification is dismissed, but my app is not brought to front. – jwBurnside Mar 27 '17 at 18:25
  • is DetailActivity your launcher activity ? or its a class within your app ? – Gautam Mar 27 '17 at 18:32
  • Detail activity is not my launcher, it is a class within my app. – jwBurnside Mar 27 '17 at 18:34
  • 1
    even i am using OneSignal but I use deep link schema to open my activities. You can give it a try & startActivity call should be preceded by context which is not there in your mentioned code. You can try that too - i.e getApplicationContext().startActivity(intent); without that startActivity won't get resolved. – Gautam Mar 27 '17 at 18:38
  • Aight cool that worked. If you want to throw it into an answer I'll give you credit for it. – jwBurnside Mar 27 '17 at 18:40

2 Answers2

2
  1. Try setting appropriate flags with intent intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  2. StartActivity call should be preceded by context which is not there in your mentioned code. You can try that too - i.e getApplicationContext().startActivity(intent); without that startActivity won't get resolved
Gautam
  • 3,252
  • 3
  • 23
  • 32
0

You need to put this in your intent before start the activity:

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Then start the activity.