I am working on project where we are showing notifications for each photo album updates. But on tap of one notification, it is removing all notifications from the notification bar.
I tried the following code:
private void handleNotification(NotificationModel notificationModel) {
// display the notification in the notification bar
int i=0;
if(!TextUtils.isEmpty(notificationModel.getAlbum_id()))
i=Integer.parseInt(notificationModel.getAlbum_id());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//icon appears in device notification bar and right hand corner of notification
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(MainActivity.BUNDLE_ALBUM_ID_KEY, notificationModel.getAlbum_id());
intent.putExtra(MainActivity.BUNDLE_ALBUM_NAME_KEY, notificationModel.getAlbum_title());
/*intent = new Intent(this, NotificationActivity.class);*/
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),i,intent,PendingIntent.FLAG_UPDATE_CURRENT);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
/*builder.setAutoCancel(true);*/
// Large icon appears on the left of the notification
builder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher));
// Content title, which appears in large type at the top of the notification
builder.setContentTitle(notificationModel.getTitle());
builder.setContentText(notificationModel.getMessage());
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=builder.build();
notification.defaults = Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(i,notification);
}
What am I doing wrong?
Please check value of "i" that is working as notification id remain same each time that might be problem ...
Here
int i=0;
if(!TextUtils.isEmpty(notificationModel.getAlbum_id()))
i=Integer.parseInt(notificationModel.getAlbum_id());
Please check value of "i", I think its always same in your case that you are using with below code
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),**i**,intent,PendingIntent.FLAG_UPDATE_CURRENT);
And
notificationManager.notify(i,notification);
Related
I have a notification that have 2 actions. One intent open an Activity, the other open browser web.
my code:
Intent intent1 = new Intent(context, NotificationClickActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
String url = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(shortDescription)
.setStyle(new Notification.BigTextStyle().bigText(longDescription))
.setSmallIcon(R.drawable.small_defaulticon)
.setAutoCancel(true)
.setContentIntent(pIntent1)
.addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
.setContentIntent(pBrowserIntent)
.addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
If i click on the notification it goes away, but if I click on one of the 2 Actions it open the right intent but notification doesn't cleared.
I try also .setDeleteIntent(myPendingIntent) but nothing... I wonder where I am going wrong...
A screen of my notification...
Thank you!
A notification is never dismissed when an action button is pressed. It just sends you associated intent. If you want it to dismiss, you need to call NoficationManager.cancel(int id) to cancel it yourself. You will normally do it in the same method, which handles action button intent.
Notification.FLAG_AUTO_CANCEL flag you tried to use is only applied to notification body and not to action buttons.
use cancel() in the activity that started by your action to dismiss the notification
try this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL);
and try to send uniqueId in notificationManager.notify(uniqueId, noti);
I use the following code to show the notifications
private void SendNotification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");
//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}
Each time a new notification comes, the previous one should be removed and the new one should be shown.
Any clues will be helpful.
Thanx in advance
o.
You are generatting a notification with different id each time (System.currentTimeMillis()), change it for a single ID.
notificationManager.notify(0, notification);
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.
NotificationManager.html
There is one way that you should clear all Notification of your Application before you post a newer one.
Give it a shot like,
notificationManager.cancelAll();
To clear a particular Notification you can do it like,
notificationManager.cancel(notification_id);
I wrote an app that saves local notification at specific time in future and when that time is reached it shows notification in status bar even if the app is not running. My question is: it is possible to show some additional information to user when he clicks on notification in status bar?
Yes it is possible to display additional information when user taps on that particular notification. While writing code to display notification, you will find one setLatestEventInfo method. Use it. Its exactly what you need.
public void createNotification(View view) {
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
notificationManager.notify(0, notification);
}
I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).
I just want to make a custom notification on statusbar that remanis there during user interaction.
I use this code:
Notification notification = new Notification(getBatteryStateResource(percent,status), "notify",System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.statusbar);
Intent intent = new Intent(mContext, Activity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
notification.contentIntent=contentIntent;
notification.contentView.setOnClickPendingIntent(R.id.imageView, anotherContentIntent);
NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(42, notification);
This code work only on android 4 smartphones. On tablets, every time I click on the imageView of the notification, system delete notification.
Instead, on Android 2.3 and 2.2 the "anotherContentIntent" don't start and start only "contentIntent", why??
many thanks at all....
Use FLAG_NO_CLEAR in addition to FLAG_ONGOING_EVENT.