Each time I send a notification the device acts as if it the notification has been sent an infinite amount of times. That is, the ringtone and vibrate go off continually until I open the notification menu. I can get this behavior to stop if I comment out the notification.flag |= line but then I am unable to use the auto-cancel flag. Any ideas on what is causing this?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(resourceID)
.setContentTitle(title)
.setContentText(contentText)
.setSound(soundUri, RingtoneManager.TYPE_NOTIFICATION)
.setVibrate(CommonUtilities.VIBRATE_PATTERN);
Notification notification = mBuilder.build();
/* comment this out to stop the infinite notification loop */
notification.flags |= Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
Intent intent = new Intent(this, DashboardActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, CommonUtilities.REQUEST_NOTIFICATION, intent, 0);
String pref = getSharedPreferences(CommonUtilities.NOTIFICATION_PEF, MODE_PRIVATE).getString("incoming", contentText);
notification.setLatestEventInfo(this, title ,pref, activity);
notification.number += 1;
//Display notification
notificationManager.notify(0, notification);
Hmm well you should check your flags again!
FLAG_ONLY_ALERT_ONCE
Bit to be bitwise-ored into the flags field that should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
So my guess is that with this flag you will get your notification to vibrate all the time.
From the official docs http://developer.android.com/reference/android/app/Notification.html
Related
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);
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);
just a simple question: How to make the single-line notification that will disappear after a short time.
Just like Whatsapp's:
My current notification code is still very basic:
Intent intent = new Intent("com.example.MyChat");
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(context)
.setContentTitle("New message from " + name)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
.getNotification();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
Thanks
This is called the "ticker" and it is shown briefly by the status bar if you set the tickerText property of your Notification.
See setTickerText.
You can use AlarmManager to invoke after a short time, say 5 seconds.
Also you can use a service, which gets launched, when first time the notification appears and it could cancel the notification after some time.
here you can cancel Notification using,
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTFICATION_ID);
Have a look at this post, it might help you as well.
I am using following code for notification. It should vibrate and make sound at the time of event. But it is giving sound when the notification is creating, although the notification time is after 30 min.
final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",nextAlarmTime);
Context context = getApplicationContext();
CharSequence contentTitle = "Viramune";
CharSequence contentText = notificationAlart;
Intent notifyIntent = new Intent(context, Myapp.class);
PendingIntent intent1 = PendingIntent.getActivity(ViewDoughnut.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent1);
notifyDetails.flags = Notification.FLAG_ONLY_ALERT_ONCE;
notifyDetails.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
mNotificationManager.notify((int) editEventid, notifyDetails);
What is the problem in my code?
Android does exactly what it told to do:
mNotificationManager.notify((int) editEventid, notifyDetails);
This line creates a notification. You should use AlarmManager to schedule your notification in future.
The third parameter to the Notification constructor is not used to determine when to shoot the notification, but is just used for display and sort.
I believe you're trying to do something that needs to be done using AlarmManager, and not Notification.
I have read many examples of how to create notification messages.
What i wanted to achieve, is because the notification will be executed by a widget, i would like
the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to.
The notification for my purposes will just plainly notify, nothing else.
So what would be the code of an intent that just clear/cancel itself.
The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Thanks
Check out FLAG_AUTO_CANCEL
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
EDIT :
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set the flags in notification.flags instead of notification.defaults.
Example:
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
The only way I can see of doing this is to have your Notification's Intent point to a background Service. When this Service is launched, it would clear the given Notification using NotificationManager.cancel(int id). The Service would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.
/**
Post a notification to be shown in the status bar.
Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
*/
notificationManager.notify(NOTIFICATION_ID, notification);
/**
Cancel a previously shown notification given the notification id you've saved before
*/
notificationmanager.cancel(NOTIFICATION_ID);
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
For example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.