2017-10-09 64 views
2

這裏我顯示從Service類的Android推送通知,這是顯示罰款和獲得解僱,如果我們點擊完整Remoteview但當我打開ActivityRemoteView的按鈕/文本中,通知不會被取消。取消不工作在活動取消特定的推送通知android

在服務:

Intent notificationRecordFollowupintent = new Intent(this, RecordFollowUpActivity.class); 
notificationRecordFollowupintent.putExtra("notify_id", m); 
PendingIntent contentRecordFollowupIntent = PendingIntent.getActivity(this, m, notificationRecordFollowupintent, PendingIntent.FLAG_UPDATE_CURRENT); 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     builder.setSmallIcon(R.mipmap.ic_launcher) 
       .setWhen(System.currentTimeMillis()) 
       .setDefaults(NotificationCompat.DEFAULT_SOUND) 
       .setStyle(bigTextStyle) 
       .setCustomBigContentView(contentView) 
       .setCustomContentView(contentView) 
       .setGroup("Follow Up Alert") 
       .setNumber(++numMessages) 
       .setOngoing(true) 
       .setAutoCancel(true) 
       .setContentIntent(contentRecordFollowupIntent); 
    } else { 
     builder.setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Follow Up Alert") 
       .setContentText("" + name) 
       .setWhen(System.currentTimeMillis()) 
       .setDefaults(NotificationCompat.DEFAULT_SOUND) 
       .setStyle(bigTextStyle) 
       .setCustomContentView(contentView) 
       .setCustomBigContentView(contentView) 
       .setContentIntent(contentRecordFollowupIntent) 
       .setAutoCancel(true); 

    } 

    Notification notification = builder.build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    nm.notify(getResources().getString(R.string.app_name), m, notification); 

在活動時間:

Intent intent = getIntent(); 
    int notificationId = intent.getIntExtra("notify_id", -1); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(notificationId); 

我得到相同的ID也仍然不會被取消。

+0

你可以分享你的代碼來創建通知嗎?它在服務中是否使用與變量'm'相同的ID?您正在使用'm'作爲PendingIntent的'requestCode',但是我看不到'm'是否被用於'notificationManager.notify()'上的實際'notificationId'。 – Grimthorr

+0

當然,檢查我更新的問題 –

+0

謝謝,這似乎應該工作!服務如何決定何時創建通知?它可以重新創建它比它可以取消更快嗎? – Grimthorr

回答

-1

NotificationManager.cancelAll()刪除所有通知。 NotificationManager.cancel(notificationId)刪除特定的通知。

+1

我想你已經無法正確讀取我的問題。請再讀一遍。 –