2016-09-21 55 views
-4

中的通知ID我正在製作一個基於通知的應用程序,在該通知中我實現了通知代碼,但是代碼中我想提供有關通知ID的信息,什麼是任何人可以解釋我的通知。什麼是android

代碼: -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    mBuilder.setContentTitle("This is a notification"); 
    mBuilder.setSmallIcon(R.drawable.app_me); 
    mBuilder.setContentText("You have successfully created notification"); 

    Intent resultIntent = new Intent(this,ResultActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(ResultActivity.class); 

    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPending = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(resultPending); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // notificationID allows you to update the notification later on. 
    notificationManager.notify(notificationID, mBuilder.build()); 

回答

0

當您需要爲同一類型的事件多次發出通知時,應該避免發出全新的通知。相反,你應該考慮更新以前的通知。 爲此您需要NotificationID。 通知可以通過調用 入住這裏, https://developer.android.com/training/notify-user/managing.html

+0

進行更新,以通知ID問題,它和我的代碼運行成功完全 – Satish

+0

但是當我點擊通知將被重定向到結果屏幕,但通知仍會出現在狀態欄 – Satish

+0

您的代碼將運行。如果您需要更新/刪除創建的通知手段。您需要使用NotificationID。 –

0

每個通知都有自己的ID。如果您知道該ID,則可以稍後更新或取消通知。

0

它是您要定義的數字ID。如果您稍後想要更改此通知,則可以指定相同的通知ID,並且它會更改原始通知(或者如果需要,可以取消它)而不是創建新通知。

0
notificationManager.notify(notificationID, mBuilder.build()); 

notificationID是你可以發送任何整數變量,使其獨特的。當您必須取消通知或需要更新時,才需要進行更新。 可以代替通知的ID給任何價值像下面

Random notification_id = new Random(); 
notificationManager.notify(notification_id.nextInt(100), mBuilder.build()); 

,或者您也可以爲它設置靜態整數值。