Android click on notification does not open the attached Activity - android

I want to open an Activity when I click on the notification from the Status bar. I have seen this answered on StackOverflow but none of these answers work for me.
This issue is only seen on Lollipop devices and best way to reproduce is:
1. launch the app.
2. Back ground the app.
3. Receive a push notification.
4. Click on the notification and try with 3-4 push notifications.
Below is my code: I have also tried the following:
adding Intent.ACTION_MAIN
Intent.CATEGORY_LAUNCHER to the intent
PendingIntent.FLAG_UPDATE_CURRENT.
android:exported="true" in my AndroidManifest.xml
but nothing works. I am seeing this issue only on Lollipop devices.
Please note I see this issue intermittently. Is this something related to the intent getting cached and not getting delivered. Please help.
PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(payload.getMessage())
.setTicker(payload.getMessage())
.setContentIntent(pendingIntent)
.setSound(soundUri);
private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(context, DeepLinkActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage(BuildConfig.APPLICATION_ID);
intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}

try adding Intent.FLAG_ACTIVITY_CLEAR_TASK flag in the intent
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this seems to solve my problem

Have you set the pending intent in the notification setContentIntent method?
Try this, it works for me on all api versions
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getResources().getString(R.string.notification_title))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

Related

Only one Notifcation is clicable from a few

The problem is: in my code im sending 4 notification. But only one top notifacation opens my Activity another 3 do nothing on click.
Here is my code to send Notificaiton. I do it 4 times in my program.
The Notification ID is always diferrent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.element)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messages.get(messageCount).toString());
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(messageCount, notification);
Please Help . Thanks!
The problem is that android "eat" the same intents from pending intent.
You need to make them different using different extra and/or request codes

Notification is not cancelled after calling notification.cancel

I create notification with action button. When clicking on action button, broadcast receiver is called. I am passing the notification ID in the intent
In the broadcast receiver I do the following
int notifId = intent.getIntExtra(Constants.NOTIF_ID, 0);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifId);
This is how I generate the notification
int notifId = Util.random.nextInt(9000);
Intent mIntent = new Intent(con, NotificationBroadcastReceiver.class);
mIntent.putExtra(Constants.NOTIF_CODE, codeReason);
mIntent.putExtra(Constants.NOTIF_ID, notifId);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(con, 0, mIntent , 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.icon)
.setContentTitle("test")
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "action", mPendingIntent);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(con, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
con, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS
);
// mId allows you to update the notification later on.
mNotificationManager.notify(notifId, mBuilder.build());
However the notification does not get hidden/dismissed although I know I am hitting the code ( using log statements).
Why is that?
I found the answer. I "think" that due to the 2 pending intents having the same con and req code, they are ending up modifying their intent values. I fixed it by using 2 different request codes to ensure unique Pending Intents . Therefore , I got the same notifId
Check your id if it's zero.
Check the notification is not binded with other Service by startForeground().

Intent data not updated in activity while launched from andorid push notification click

I am using Google cloud messaging for my android application.GCM part is running successfully.Now I want to show the message in a UI activity when the user taps on the notification icon in the status bar.
I am launching my ResultShowActivity from the notification using the following code.
Intent notificationIntent = new Intent(context, ResultShowActivity.class);
notificationIntent.putExtra("Message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setSound(alarmSound)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The message is being sent properly,and shown correctly when the GCM notification appears but when I capture the extras of intent in ResultShowActivity it is always showing the old message.
I am using following code to receive message from intent in ResultShowActivity.
String GCMMsg = showViewIntent.getStringExtra("Message");
I have tried removing the in ResultShowActivity by using
intent.removeExtra("Message");
or
intent.replaceExtras(extras);
Can anyone please suggest how to retrieve the updated message from intent.Thanks for any help regarding this.
Please use following code
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(this,iUniqueId,notificationIntent, 0);
Instead of following line of code
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Hope this will help
You need to add Intent.FLAG_ACTIVITY_SINGLE_TOP to intent flag and PendingIntent.FLAG_UPDATE_CURRENT to pending intent flag.
Intent intent = new Intent(this,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Important: Make sure to implement onNewIntent(Intent intent). This function will be called when your activity is in foreground.

Notification crashes my app

This is my code for a notification. This is fired up by a service which runs when the phone is rebooted. If the phone is rebooted and the notification needs to be fired up by the service then it will work but when you click the notification, it's supposed to open an activity (the app is not running at this point) so I think that is why it crashes. When the app is running it works fine.
Any help would be appreciated.
NotificationManager mNotificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), Notification.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent result = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
I fixed it
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
If anyone ever comes around this problem, try setting these flags.

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.

Categories

Resources