Some question about push notifications. How do I dissmis all the notifications of my app when I press one of them? Also ... how do I dissmiss the noifications if I start the app (not from the noification)?
Here is how I send a notification from my GcmIntentService class (if it is helpfull).
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION_ID = Integer.parseInt(extras.getString("id"));
Intent in = new Intent(this, PushActivity.class);
in.putExtras(extras);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
String msg = extras.getString("msj");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
in, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MY TITLE")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
I'm not sure if the notifications you are putting out are for the same event, if so you can do what is called "stacking notifications" like Gmail does when you get a second or third email. It's all in the one notification and therefore will go away as one. This is only compatible above 4.1.
This is the code Google gives along with the stacking. This any help?
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
try android.app.NotificationManager.cancelAll()
Related
I'm using the following to code to send a notification:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Title")
.setContentText("Message");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(getBaseContext(), MainActivity4.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
The code works as I expected except for one little thing, once I go to the notification bar, expand it and choose the notification, it keeps staying on the notification bar, opposite to the behavior of most notifications, than once you tap them they disappear from being there.
Guess it must be some option in the configuration of the notification but as much as I search I cannot find it.
Hope you can help.
Are you looking for this?
.setAutoCancel(true)
so you would have
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Message");
I am new to android.Can someone show me detail code to make app display notification in certain time of day?
It will be better if you show the code in detail.
I only know to use the local notification.
code is here.
long when = Calendar.getInstance().getTimeInMillis();
when += 10000;
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500};
NotificationCompat.Builder notification =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification1)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(pendingIntent)
.setLights(Color.BLUE, 500, 500)
.setAutoCancel(true)
.setVibrate(pattern)
.setDefaults(Notification.DEFAULT_SOUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, notification.build());
Take a look at AlarmManager
https://developer.android.com/reference/android/app/AlarmManager.html
AlarmManager allow you to schedule your application to be run at some point in the future
i have a notification manager that launches an activity when clicked.
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setAutoCancel(false);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Launch.class).putExtra("type", 0), 0);
mBuilder.setContentIntent(pendingIntent);
the function performed in this can also be be asynchronous depending on the users preference. I want to update the notification to an ongoing progress. something of this nature:
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle("Sending details")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setProgress(100, 0, true)
.setOnlyAlertOnce(true)
.setAutoCancel(false);
when the notification is selected. This is achieved but what i want to do is update the notification from the former to the later without having to first slide up the navigation drawer.
Try this..
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
you need NOTIFICATION_ID to updated the specific notification.
Please refer this
As Richard said in this question > Possible to continuously update text in the notification area? , I have the same issue here .
I want to update text in the notification area .
This question is answered and accepted , but the answer doesn't help me .
It's about to create text as bitmap dynamically and set it to Small Icon of notification .
But as Richard commented , the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly.
Please kindly show me the right way to do this stuff .
It's quite simple to update your Notification.
First of all when you create it you must create it with an id.
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.ic_notify);
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
Now to update your Notification you only need to notify the new notification with the old id and this will be updated (you don't need to reset the paraneter you don't want to update).
mNotifyBuilder.setContentText(setContentText("New Text")
mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
//this update your notification
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
I tried to make one but it has one disadvantage is that, it has one empty notification in notification drawer,
public static int when = 0;
private void generateNotification(Context context) {
Log.i(TAG, "generateNotification");
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
this)
// .setContentTitle("AppName")
// .setContentText("Message For Notification Drawer")
// .setSound(soundUri)
// .setDefaults(Notification.DEFAULT_SOUND)
// .setVibrate(new long[] { 1000, 1000 })
// .addAction(R.drawable.ic_launcher, "View", pIntent)
// .addAction(0, "Remind", pIntent)
// .setNumber(fCount)
// .setWhen(when)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(Integer.toString(when)) // Set String you want
.setAutoCancel(true)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// notificationManager.notify(when, mNotification.build());
notificationManager.notify(1, mNotification.build());
when++;
}
I want to close the notification when the user selects the notification.This is my code that i am using to show the notification to the user.I am using this in GCM (Google Cloud Messaging)
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, DemoActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle(" Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg.toString());
mBuilder.build().flags = Notification.FLAG_AUTO_CANCEL;
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(notificationSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
But this is not working fine for me when i am selecting the notification it is opening the activity but it is not closing the notifications .Please suggest me what went wrong in this
Try
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID); // use this to cancel notification
Or you can try
mBuilder.setAutoCancel(true); //so the notification is automatically canceled when the user clicks it in the panel
Just remove these lines :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, DemoActivity.class), 0);
And
mBuilder.setContentIntent(contentIntent);
Use setAutoCancel (boolean autoCancel)
Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.
Reference:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setAutoCancel(boolean)
Notification notification = new Notification(
YOUR_DRAWABLE_RESOURCE, "Message",
System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
You can use the FLAG_AUTO_CANCEL to cancel notfication when selected.