【问题标题】:Android Multiple NotificationAndroid 多重通知
【发布时间】:2011-03-08 11:34:20
【问题描述】:

我正在开发一个应用程序,用户可以在其中创建事件并为该事件设置通知。所以我想添加多个通知。我正在使用以下代码。

final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",calendar.getTimeInMillis());
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, ViewDoughnut.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ViewCal.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

当我使用上述代码添加事件并创建通知时,它工作正常。但是如果我添加另一个事件,没有创建新的通知,旧的只是更新。我想再添加一个通知。怎么做?此外,如果用户删除与之对应的事件,我想删除任何特定通知。怎么可能?

【问题讨论】:

    标签: android notifications android-intent


    【解决方案1】:

    我假设SIMPLE_NOTIFICATION_ID 是一个常数?要获得单独的通知,您需要为每个通知使用不同的 ID。

    【讨论】:

    • 但是当用户删除相应事件时如何取消通知呢?如果我尝试使用 mNotificationManager.cancel(editEventid),它根本不起作用。
    • @user525004 如果您将创建通知时使用的相同 ID 传递给取消方法,那么它应该可以工作(在我的应用程序中对我有用)。如果您的事件已经有自己的唯一 ID,只需将其用作通知 ID。
    • 如何使ID不同?每次自己创建通知时 +1,你能告诉我一些代码吗?
    • 新随机(System.currentTimeMillis()).nextInt()
    【解决方案2】:

    下面是传递唯一通知ID的代码:

    //"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences.
    String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0");
    int notificationIdinInt = Integer.parseInt(notificationId);
    
    notificationManager.notify(notificationIdinInt, notification);
    
    // will increment notification id for uniqueness
    notificationIdinInt = notificationIdinInt + 1;
    CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + "");
    //Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences.
    

    如果您需要更多详细信息或有任何疑问,请告诉我。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多