4

我想在用戶點擊通知時自動取消通知。以下代碼適用於除Android棒棒糖設備以外的所有設備。在棒棒糖設備中,通知只在用戶滑動時才進行。通知自動取消不適用於Android棒棒糖

@SuppressWarnings( 「棄用」) 公共無效sendNotification時(INT ID){

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Test") 
      .setContentText("Jump to next screen") 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setAutoCancel(true); 

    mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE; 


    // Creates an explicit intent for an Activity in your app 
    Intent resultIntent = new Intent(this, NextActivity.class); 

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
            resultIntent, 0); 
    //PendingIntent.FLAG_UPDATE_CURRENT 

    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // id, if there is a need to update the notification later on. 
    mNotificationManager.notify(id, mBuilder.build()); 

    Log.v(TAG, "Notification ID value " + id); 

    //mNotificationManager.cancel(id); 
} 

缺什麼在此代碼?

+0

即使我看到這個問題的特殊情況。你能找到解決辦法嗎?我看到的是,它被刪除,並且在通知中心發生任何刷新。 – Ayyappa 2015-03-20 16:45:21

+0

正在關注。我也被卡住了。看了這麼多,但沒有解決這個問題呢。我目前正在運行計時器,並在計時器耗盡後取消所有通知。 – LokiDroid 2015-03-26 07:10:43

回答

0

對我很好。我的自動取消仍然適用於5.0.2。讓我給你的我的代碼的一部分:

public static void notif(int id, String title, String text, Context context, Class activity) { 
    // get an instance of the NotificationManager service 
    if (mNotifyMgr == null) 
     mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent intent = new Intent(context, activity); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

    PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(context) 
      .setSmallIcon(R.mipmap.launcher) 
      .setContentTitle(title) 
      .setContentText(text) 
      .setContentIntent(notifIntent); 
    mNotifyMgr.notify(id, mBuilder.build()); 
} 
0

修改如下: setAutoCancel(真) - > setAutoCancel(假); 然後在行動活動做以下

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancelAll(); 

,如果你想設置可以設置通過

intent.putExtra("key", "value")