I am having an issue where, no matter what notification the user touches, I am always receiving the same notification, even though it is cancelled.
The first notification a user touches is the only notification that will ever be delivered to my pending intent, until device reboot (then the first touched notification becomes the new Hotel California notification).
Here is how I am constructing the notifications:
final Bundle extras = new Bundle();
final int notificationId = Integer.parseInt(payload.getObjectId());
extras.putInt(NOTIFICATION_ID, notificationId);
final Intent intent = new Intent(context,
PushNotificationLauncherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(extras);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setContentTitle(context.getString(R.string.app_name));
builder.setContentText(payload.getText());
builder.setSmallIcon(R.drawable.icon_launcher);
builder.setAutoCancel(true);
final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
intent, 0, extras);
builder.setContentIntent(pIntent);
final Notification notification;
if (Build.VERSION.SDK_INT < 16) {
notification = builder.getNotification();
} else {
notification = builder.build();
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationId, notification);
Then I have this in my PushNotificationLauncherActivity:
final Bundle extras = getIntent().getExtras();
final int notificationId = extras
.getInt(SocialcastGoogleCloudMessageReceiver.NOTIFICATION_ID);
final NotificationManager notificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
// please go away
notificationManager.cancel(notificationId);
Log.d(PushNotificationLauncherActivity.class.getSimpleName(),
"Touched notification ID: " + String.valueOf(notificationId));
No matter which notification I touch, in the log Touched notification ID: 1234 will always show, even if the notification I touch on has an ID of 56789.
I highly doubt it is an issue with my test-device, but incase it helps; I am using a 2013 Nexus 7 with KitKat 4.4.4 with the Dalvik runtime (not ART) Build KTU84P.
Pass your notificationId as the request code here:
final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
intent, 0, extras);
Change this to:
final PendingIntent pIntent = PendingIntent.getActivity(context, notificationId,
intent, 0, extras);
When comparing two Intent instances, the included Bundles are not compared. So, from android's point of view, the two intents are the same.
Interestingly, the reqCode (second argument in PendingIntent.getActivity(...)) is compared. And this should render your Intents unique.
Related
After looking at the official document of the Android studio, I created a program that alerts me according to the situation I wanted.(Actually, I just copied and pasted it.)
However, notifications only appear in the top bar now.
I'd like to have a notification window appear on the lock screen where my phone is turned off.
A notification sound is coming when the cell phone is turned off.
Do I need to design a new window in xml?
Or is there a problem?
I'm sorry if the question is weird.I look forward to your advice. Thank you.
Here is my code (Notification Method)
private void showNoti() {
builder=null;
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
manager.createNotificationChannel(
new NotificationChannel(CHANNEL_ID,CHANEL_NAME,NotificationManager.IMPORTANCE_DEFAULT)
);
builder = new NotificationCompat.Builder(this,CHANNEL_ID);
}else{
builder=new NotificationCompat.Builder(this);
}
Intent intent = new Intent(this, mapActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent fullScreenIntent = new Intent(this, mapActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent snoozeIntent = new Intent(this,mapActivity.class);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID,0);
PendingIntent snoozePendingIntent =
PendingIntent.getBroadcast(this,0,snoozeIntent,0);
builder.setContentTitle("화재알림");
builder.setContentText("화재가 발생했습니다");
builder.setSmallIcon(R.drawable.fire);
builder.setContentIntent(pendingIntent);
builder.addAction(R.drawable.fire, getString(R.string.app_name),
snoozePendingIntent);
builder.setFullScreenIntent(fullScreenPendingIntent, true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setAutoCancel(true);
Notification notification = builder.build();
manager.notify(1,notification);
}
I will try to create multi local notification every time with different parameters this is my code to create the notification:
public void setNotficition(int Time,String ProdName,String ProdDesc,String ProdID){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("ProdName",ProdName);
notificationIntent.putExtra("ProdDesc",ProdDesc);
notificationIntent.putExtra("ProdID",ProdID);
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, Time);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),broadcast);
}
This is my Code for BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
String ProdName= intent.getStringExtra("ProdName");
String ProdDesc= intent.getStringExtra("ProdDesc");
String ProdID= intent.getStringExtra("ProdID");
int ID = Integer.parseInt(ProdID);
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle(ProdName)
.setContentText(ProdDesc)
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
}
Every time its take the last call
Notification id should be unique within your application.
If a notification with the same id has already been posted by your
application and has not yet been canceled, it will be replaced by the
updated information.
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(UNIQUE_ID, notification);
If you are using PendingIntent.getBroadcast() method, use different requestCode for different notification:
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("ProdName",ProdName);
notificationIntent.putExtra("ProdDesc",ProdDesc);
notificationIntent.putExtra("ProdID",ProdID);
PendingIntent broadcast = PendingIntent.getBroadcast(this, REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Hope this will help!
You can create multiple notification by changing notification id, everytime.
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
The second parameter of the getBroadcast call is "notification id" (i.e. 100 in your case). Just use different different notification id in case of generating multiple Notifications.
Hope it will help u :-)
A notification is created and linked to a id, this id can be used to modify or update on existing notification, weather you are changing the intent or just the behavior of the stack.
i trying do a "read more" after user click on notification.
What i need, start new Activity with unique extra content "id", "title" and "text".
My code:
void notificationSend(String id, String bar, String title, String text, String image, int delay) {
Bitmap bitmap = getBitmapFromURL(image, 130);
if(bitmap == null){
bitmap = getBitmapFromURL("https://pp.vk.me/c621316/v621316641/119d5/HB9s2z5mX-s.jpg", 130);
}
Intent notificationIntent = new Intent(this, SingleNewsActivity.class);
notificationIntent.putExtra("id", id);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("text", text);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.ic_bitcoin_notification);
builder.setLargeIcon(bitmap);
builder.setTicker(bar);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setWhen(System.currentTimeMillis());
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
try {
notificationManager.notify(new Integer(id), notification);
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
And it do next thing, lets send 2 notifications, after i click on first, activity started but with extra data from second notification, why?
The issue is due to using : FLAG_UPDATE_CURRENT
if you see document for this flag:
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
in your case, you are updating just extra and due to that, the value of extras get updated.
For solution of your issue, you should pass request code unique for all case:
Currant code:
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Actual:
PendingIntent.getActivity(this, unique_code, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
So The reason is that you are setting unique id to the notification, lets suppose id value is 1 when your 1st notification comes then the notification object is created with that id, when you second notification come then your id is again 1 so your notification is updated and when you click on it you get the second notification.
what you can do is set the different id for notifications. have a look at below code
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
write below code inside a function
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
Intent intent = new Intent(this, SingleNewsActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra("id", id);
bundle.putExtra("title", title);
bundle.putExtra("text", text);
intent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setStyle(new NotificationCompat.BigTextStyle().bigText("ASD"))
.setContentText("Summary").setAutoCancel(true);
mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d("", "Notification sent suessfully.");
in the above code whenever you will receive notification you will get a unique code in NOTIFICATION_ID
I have an activity (Musicplayer.class) which plays music and a notification is shown when the track starts playing however when I tap on the notifaction it takes me back to the main activiy (1st activiy of the app)
How do I modify the code so that tapping the notification takes me to the music player activity.
the code is taken from the android documentation ( see below )
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MusicPlayer.this)
// set values....
Intent resultIntent = new Intent(MusicPlayer.this, MusicPlayer.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MusicPlayer.this);
stackBuilder.addParentStack(MusicPlayer.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final int mId = 1;
mNotificationManager.notify(mId,mBuilder.build());
changed code to this:
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pendingIntent);
and now the activity duplicates (two same activities running, same music playing 2 times)
I am writing an application and in this application I need to set multiple notification with same intent just like open my application whenever user tap on any notification.
All the notification comes on different time and date without having any data but the problem is, if I set two notification for 03:27 PM and 03:28 PM then the first notification (03:27 PM) is canceled (Overwritten by second) and second is working correctly.
I am currently using this code for achieving this goal:
this method is used to set notification from Activity:
public static void setNotificationOnDateTime(Context context, long fireTime)
{
int requestID = (int) System.currentTimeMillis();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NotificationReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, requestID, i, 0);
am.set(AlarmManager.RTC_WAKEUP, fireTime, pi);
}
and my NotificationReceiver class look like this:
NotificationManager nm;
#Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "Notification fired...");
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent;
Bundle bundle = intent.getExtras().getBundle("NotificationBundle");
if(bundle == null)
{
contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, SplashScreen.class), 0);
}
else
{
contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MenuScreen.class)
.putExtra("NotificationBundle", bundle), 0);
}
Notification notif = new Notification(R.drawable.ic_launcher,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, "Me", "Message test", contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
I alerady spend a lot time on googling and found some solutions but they didn't work for me here is the link of some of them:
android pending intent notification problem
Multiple notifications to the same activity
If any one knows how to do this please help me.
Quote:
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
But you're always using 1 for the id parameter. Use a unique ID when you post several notifications.
Update If that doesn't help, you can still create Intents which do not compare as being equal, while having an equal effect.
this may be helpful
notif.flags |= Notification.FLAG_ONGOING_EVENT;
the notification will never close...
Try to set as below :
contentIntent=PendingIntent.getActivity(p_context, i, new Intent(context, MenuScreen.class),PendingIntent.FLAG_CANCEL_CURRENT);
It might help you.