[英]In Android how can i know the current notification id to clear the notification
現在在 android 中,我將這段代碼放在一個活動中,以便在按下按鈕時顯示通知。
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)
現在我把這段代碼用來清除通知,但它只清除 id 為 0 的通知,所以我怎樣才能清除當前按下的通知
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 清除通知。
您應該在通知中添加標記,然后通過提供正確的ID和正確的標記來清除通知。
如果您傳遞相同的ID,則不需要通知計數器,因為當通知看到相同的ID時,它會清除舊通知並添加新通知,除非您希望顯示該用戶收到多個通知。
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);
}
取消通知只需使用此方法:
//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);
}
你需要在創建通知時添加這行代碼!
notification.flags |= Notification.FLAG_AUTO_CANCEL;
這將取消點擊通知。
進一步閱讀: 點擊通知后打開應用程序
**編輯,添加一個額外的內容以確定是否點擊了某個通知pIntent.putExtra("fromNotification", true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.