2017-05-09 98 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