Notification looses track of activity when cleared from recent apps - android

My activity looses the track of its notification when the application is removed from the recent apps
My notification code :
builder = new NotificationCompat.Builder(this).setContentTitle("Started").setSmallIcon(R.drawable.ic_launcher);
targetIntent = new Intent(getApplicationContext(), MainActivity.class);
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1234, builder.build());
I have to restart the app after that and the notification doesn't open the activity any more.
Any leads on this ???

Related

The other notifications is lost the click behaviour, when a notification is chosen at device's system tray in android

If there're two my app's notifications at device's system tray, the user choose one notification, the app's opening but all of other notifications is lost the click behavior, it means user cannot click them anymore. How can I prevent this action?
My code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(getString(R.string.test))
.setContentText(messageBody);
Intent intent = new Intent(this, NotifActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(50) + 1, builder.build());
int mId = (int) System.currentTimeMillis();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.imagename)
.setContentTitle("Title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, activityname.class);
//activityname is the name of activity which you want to launch
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I detected my root cause in below code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Because the request code is the same for all notifications, so when we click to the one notification, the other lost their click listener. Fix like below codes
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);

How to open app in notification in android?

In my app, when completing a task in async file, it shows a notification with this code
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, new Intent(), 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
The problem is when I click the notification, nothing happens. Basically I want it so that, if the app is already open and the user clicks the notification, then nothing should open. If the app is not open (which means its still running but minimized) then I want it to open the app, like maximize it.
Does anyone know how to do this?
Thanks
You need an Intent, sample code:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
This will open your MainActivity

Push notification open Activity with same intent

I receive push notifications with data(intent). If I received two or more notifications with different ids, but open one Activity and ids are same. For example, I receive three notifications with different id=1,2,3. But when Activity is started use one id = 3. When I click first or second notification with ids 1 and 2, open Activity with id 3.Can you help understand my mistake in the code?
NOTIFICATION_ID ++;
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(msg.getString("title"))
.setContentText(msg.getString("message"))
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(Picasso.with(getApplicationContext()).load(msg.getString("icon")).get()).setSummaryText(msg.getString("message")))
.setAutoCancel(true);
Log.e("msg---",msg.toString());
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(this, ActivityDetail.class));
// intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Keys._PostId,msg.getString("id"));
intent.putExtra(Keys._Image, msg.getString("icon"));
intent.putExtra(Keys._PostType, msg.getString("post_type"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Create your PendingIntent like this and do a trick
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT);

android clear intent which send from notification

My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.

Notification not starting a activity in android

I am developing a small app in which I want a background service to send a notification and when the user clicks the notification it must launch the appropriate activity. I am posting the code here for the notification and my problem is that the notification gets displayed on the status bar but when i click it it does not launch the activity. Can somebody suggest where I am going wrong. Please help.
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotificationService.this,
com.jeris.android.MetroActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(NotificationService.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(NotificationService.this, "Bullion",
"Rates for "+ parsedDate+"has not been updated. Go to app to check rates",
pendingIntent);
notificationManager.notify(11, notification);
Replace
PendingIntent.getService(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
With
PendingIntent.getActivity(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notice the getService is replaced with getActivity.

Categories

Resources