2017-05-09 96 views
1

您好我已經創建了一個通知類從中我可以創建多個通知這樣一個特定的通知的ID:如何獲得

int id=0; 
id++; 
notification = new NotificationCompat.Builder(this) 
      .setContentTitle(title) 
      .setSmallIcon(icon) 
      .setContentText(dataNotes) 
      .setWhen(time) 
      .setAutoCancel(false) 
      .setOngoing(true) 
      .addAction(action) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(dataNotes)) 
      .build(); 

      nm.notify(id,notification); 

所以,我的問題是怎樣才能得到每一個通知的ID,我create.I想要在取消特定通知時使用該ID。 我知道StatusBarNotification包含getId()方法來獲得id,但我不知道如何實現it.Can任何人都可以幫助我。

+0

考慮在創建它們時存儲ID。然後,您可以根據需要使用大量的通知ID。可能是更好的方法,我是通知用法的新手。 – Doomsknight

+0

@DoomsKnight嗨我已經嘗試過,通過將所有id存儲在arraylist中,但進一步我沒有得到任何有關如何使用它的想法 – Adarsh

+0

如果你有一個你想取消的ID,你可以調用'cancel' 通知管理器上的方法,使用該ID。在這裏看到答案:http://stackoverflow.com/a/3595241/940834 – Doomsknight

回答

1

只需將您的通知ID聲明爲靜態即可。每次創建通知時,都會爲每個通知設置一個唯一的ID。

每次您對通知執行操作時,都會提取您的意圖ID。

創建一個通知按鈕,如下所示,對每個通知執行一些操作。

public ActionButtonConfig getEditCityButton(CountryConfig countryConfig, 
    CityConfig cityConfig, Integer notificationId) { 
ArrayList<Class> params = new ArrayList<>(); 
params.add(context.getClass()); 
params.add(countryConfig.getClass()); 
params.add(cityConfig.getClass()); 
params.add(notificationId.getClass()); 

ArrayList<Object> arguments = new ArrayList<>(); 
arguments.add(context); 
arguments.add(countryConfig); 
arguments.add(cityConfig); 
arguments.add(notificationId); 
return getActionButton(NotificationButton.ACTION, getParamArray(params), 
    arguments.toArray()); 

}

,該按鈕配置將爲您提供讓您需要在通知執行任何操作。

public static int notificationCount = 0; 

     void buildNotification(NotificationConfig notification) { 
     try { 

    buildNotification(//get items from notification config here and also notificationCount); 
      notificationCount++; 
     } catch (Exception e) { 
      Log.e("Notification builder err",e); 
     } 
     } 

在上面的代碼看到通知計數是靜態的,併產生一通知每次通知計數被更新,並且以前的值可以在以後階段今後使用。

進一步通知使用通知管理器生成通知,如您在最後一行中所做的那樣。

希望這個工程。歡呼聲

+0

非常感謝你真的幫助我:) – Adarsh

+0

很高興幫助。 – Prashast