Push notification open Activity with same intent - android

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

Related

How to close app by clicking a button in the notification?

I try to close my app after button click.
I know that i need to use pending intent and intent to do that, but i don't know how can i do it and which flags or actions can I use.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "")
.setSmallIcon(R.mipmap.ic_launcher_notification)
.setContentTitle("Close your app")
.setContentText("Your app is still running...")
.addAction(R.drawable.ic_exit,"Close App",
PendingIntent.getBroadcast(this, 0, intent, 0))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
This is not working...

local Notification on click open an activity

I have a local notification which is set in one of my receivers. Onclick of notification I want to open a specific activity. Below is my code.
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(times, builder.build());
But this is not working. On Click of notification it just removes notification from status bar. Am I missing anything ?
try this :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
instead of this :
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
use this codes before your notification builder code.
now this is the full version :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(times, notificationBuilder.build());
try to use this. i hope you will get you desire answer.
UPDATE
here is the sample project : https://github.com/ImtiazDipto01/SimpleNotification
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP as Flag on your `Intent. Like -
notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also provide unique id for notification. like -
int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Also on -
manager.notify(uniqueNotificationId , builder.build());

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

Launch activity on click notification

I want the Main Activity to be launched when the Notification is clicked. This is even when the user is not in the app. How do I do so?
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());
First of all you need PendingIntent :
Intent intent=new Intent(mContext, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notificationIntent= PendingIntent.getActivity(mContext,requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Then in your NotificationCompat.Builder add this: .setContentIntent(notificationIntent)
For unique request code use this : int requestCode=(int)System.currentTimeMillis();
You can use this in Non Activity classes too, e.g: Service, BroadcastReceiver . Then your app will be launched even it is in closed state.
//add intent and PendingIntent for open activity
Intent attendanceIntent = new Intent(getApplicationContext(), Details.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
attendanceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentIntent(pendingIntent)
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());

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

Categories

Resources