I send a few notification on the notification bar, i wanted to clear all of it when one of the notification is clicked. For now I clear one by one by using Flag. I know notificationManager.cancelAll() could clear all the notification but where should i put so that i can trigger once one of the notification is clicked.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(msgid, notification);
//notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line?
}
My solution is to call it at onResume().
#Override
protected void onResume() {
super.onResume();
// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}
You should use a pending intent that sends a broadcast and then put in place a broadcast receiver that will cancel all your notifications. It is best to memorize all notifications IDs and delete them one by one.
Related
This is my notification remote View and when I click on notification it will not clear.Please provide some solution.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the
notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
If you want that the notification will be closed after the user clicks on it, you can specify this flag in the notification builder:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
//other flags
.setAutoCancel(true);
Other option would be canceling the notification through code:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID);
just set a flag
notification.flags |= Notification.FLAG_AUTO_CANCEL;
IntentFilter filter = new IntentFilter(CONSTANT_MOTION_NOTIFICATION);
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context contextr, Intent intent) {
Intent mute = new Intent(contextr, VideoActivity.class);
mute.putExtra("url", "http://dev.galaxyweblinks.com/memorybadge/phase1/uploads/video/garage.mp4");
contextr.startActivity(mute);
notificationManager.cancelAll();
}
};
context.registerReceiver(broadcastReceiver, filter);
Create a Local broad cast receiver onReceive call the desire activity and call cancelAll() from notificationmanager
I have a few questions.
1. when I send a notice that it replaces the old.
I need to increment a counter notification.
how to do that when you click on the notification to open certain fragment?
private static void generateNotification(Context context, String message,int type) {
String title = null;
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
how to do that when you click on the notification to open certain fragment?
You cannot open Fragment directly. You need to open Activity that then will use that Fragment
Calculate the number if notification come before user last open n and increase the counter by this way.
notification.number += count(here is number of notification);`
Thanks
Can i remove this notification in a click on notication?
I can cancel notification when click on this notification using the setAutoCancel(true).
but not clear notification after click on notification.
private void sendNotification(String msg, Bundle extras) {
notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent;
if (extras != null) {
intent = new Intent(this, MessageConversationActivity.class);
intent.putExtra("sender_id", extras.get("sender_id").toString());
intent.putExtra("receiver_id", Application.ACTIVE_USER.getUser_id());
intent.putExtra("gender", extras.get("gender").toString());
intent.putExtra("profile_picture", extras.get("profile_picture")
.toString());
intent.putExtra("username", extras.get("username").toString());
} else {
intent = new Intent(this, MainActivity.class);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
String app_name = getString(R.string.app_name);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.app_icon)
.setContentTitle(app_name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
builder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Try This
builder.setAutoCancel(true);
If you set your notification to auto-cancel it is also removed once the user selects it.
OR
You can also call the cancel() for a particular notification ID to cancel notification.
notificationManager.cancel(notifyId);
Use this in the MessageConversationActivity class onCreate method.
Use notificationManager.cancel(NOTIFICATION_ID);
It cancel the notification id from the notification manager
you can do this by using
Notification.FLAG_AUTO_CANCEL
then start intent of page you want
I'm new to tabs and push notifications. I want my push notification to open up to a specific tab on my app. I have been able to achieve this, but the tab bar is missing because of the way I have implemented the intent. I'm using the TabActivity to handle the tabs. Is there a way to write the intent so it will open up the app to a certain tab?
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context, FriendGroupActivity.class), 0);
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message, contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
When you create the actual Intent you're launching with
new Intent(context, FriendGroupActivity.class)
you should just include more info either in the data URI or extras Bundle, that you then can use in your Activity's onCreate() to switch to the tab you care about
i spend a lot of time to resolve my problem. i have written a messenger client in android. my application receive income message and raise a notification. in notification bar i display each income message in a notification item. when click to notification item it will open a conversation activity to list all messages from the beginning until now. everything dose perfect but when i click another item in notification bar, nothing happen! (it must reload data for another conversation). This is my code to raise a notification:
private void showNotification(String message, Class activity, Message messageObject) {
//Get the Notification Service
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence text = message;//getText(R.string.service_started);
Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent callbackIntent = new Intent(context, activity);
if(messageObject != null)
{
callbackIntent.putExtra("conversation", MessageManager.getProvider().getConversation(messageObject.getConversationId()));
}
//callbackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int myUniqueValue = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, messageObject.getFrom(), text, contentIntent);
notificationManager.notify(messageObject.getFrom(), myUniqueValue, notification);
}
This is code block to call showNotification function
showNotification(message.getBody(), ConversationActivity.class, messageObject);
Despite your efforts to supply a unique value, those PendingIntents are all treated as the same by the system, so once you click one, the rest are rendered inert.
You'll need to add something distinguishing to callbackIntent; I suggest inventing a data URI that includes the conversation ID or something else that's guaranteed to be different for each notification (see setData).
Finally, I'd encourage you to try to collapse multiple notifications into one icon—you don't want to spam the user. See the Notifications section in the Android Design guide, under "Stack your notifications".
i changed my code and it work very well
private void showNotification(Context context, CharSequence contentTitle, CharSequence contentText, CharSequence notificationContent, Class activity, Conversation conversation) {
//Get the Notification Service
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notificationContent, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent callbackIntent = new Intent(context, activity);
if(conversation != null)
{
callbackIntent.putExtra("conversation", conversation);
}
callbackIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
int myUniqueValue = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(myUniqueValue, notification);
}