在Android中如何知道当前的通知ID以清除通知 [英] In Android how can i know the current notification id to clear the notification
问题描述
现在在android中,我将这段代码放在一个活动中,以在按下按钮时显示通知.
Now in android i put this code in one activity to show notification when a button pressed.
static int notificationCount = 0;
然后
btnNotification.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
// Construct the notification
Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
nBuilder.setContentTitle("You Have a notification!");
nBuilder.setContentText("See Your Notification");
nBuilder.setSmallIcon(android.R.drawable.btn_star);
nBuilder.setContentIntent(pIntent);
nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16
// Build the notification
Notification noti = nBuilder.build(); // required api 16
//Send it to manager
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(notificationCount++,noti);
}
}
);
从通知管理器中,我单击该通知的任何通知,都会将我重定向到另一个活动(NotificationActivity)
From Notification manager, Any notification that i clicked on it, it will redirect me to another activity (NotificationActivity)
现在,我将这段代码用于清除通知,但只能清除ID为0的通知,因此我该如何清除当前按下的通知
Now i put this code to clear the notification but it only clear the notification with id 0 so how can i clear the current pressed notification
public class NotificationActivitty extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(0);
// manager.cancelAll(); // Cancel all notifications for this app. from manager
}
如果可能,我需要通过其ID清除通知.
I need to clear the notification by it's id if it's possible.
推荐答案
您应在通知中添加标签,然后通过提供正确的ID和正确的标签来清除通知.
You should add a Tag to your notification and then clear you notification by providing correct id and correct tag.
如果您传递相同的ID,则不需要通知计数器,因为当通知看到相同的ID时,它将清除旧的通知并放置一个新的ID,除非您想表明该用户收到了多个通知.
You don't need notification counter if you pass the same id, because when notification sees the same id, it clears old notification and puts a new one, unless you want to show that user received multiple notifications.
private static final String TAG = "YourNotification";
private static final int NOTIFICATION_ID = 101;
private Notification notification;
public NotificationManager notificationManager;
//you can create notification with it's own id and tag, text, etc by passing
//these variables into the method (int id, String tag, ... etc)
public void createNotification()
{
notification = new Notification.Builder(context)
.setContentTitle("Content title")
.setContentText("Content text")
.setSmallIcon(R.drawable.your_small_icon)
.setLargeIcon(bitmapYourLargeIcon)
.setContentIntent(pendingIntent)
.addAction(R.drawable.icon, pendingIntentAction)
.build();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(TAG, NOTIFICATION_ID, notification);
}
只需使用以下方法即可取消通知:
to cancel notification simply use this method:
//clears your notification in 100% cases
public void cancelNotification(int id, String tag)
{
//you can get notificationManager like this:
//notificationManage r= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(tag, id);
}
这篇关于在Android中如何知道当前的通知ID以清除通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!