Reputation: 2197
1.open from notification bar 2.press home return to desttop 3.open from app icon
problem:2 instance of SampleTabsDefault, need to exit twice.
Intent intent = new Intent(_context, SampleTabsDefault.class);
intent.setFlags(/*Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP |*/ Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent activity = PendingIntent.getActivity(_context, 0, intent, 0);
notification.contentIntent = activity;
Upvotes: 3
Views: 1794
Reputation: 4266
Pass the content intent for notification builder using the below method
private PendingIntent getEmptyPendingIntent(Context context) {
Intent resultIntent = new Intent(context, InitializationActivity.class);
resultIntent.setAction(Long.toString(System.currentTimeMillis())); //adding unique identification for each intent
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(InitializationActivity.class);
stackBuilder.addNextIntent(resultIntent);
stackBuilder.getIntentCount();
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
return resultPendingIntent;
}
Send the notification using the below method
private void sendNotification(String title, String message) {
Intent intent = new Intent(this, InitializationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String desc = "";
if (!TextUtils.isEmpty(message)) {
try {
JSONObject jsonObject = new JSONObject(message);
desc = jsonObject.optString("payload");
} catch (JSONException e) {
e.printStackTrace();
}
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)getResources().getDrawable(R.drawable.notify_large)).getBitmap())
.setContentTitle(title)
.setContentText(desc)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(getEmptyPendingIntent(this));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Upvotes: 4
Reputation: 5203
Try below code in AndroidMenifest.xml file within your activity nod.
<activity name="SampleTabsDefault"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch="true"
>
.....
</activity>
Please read both attributes description here.
Hope this will solve your problem
Upvotes: 2