代码之家  ›  专栏  ›  技术社区  ›  Chander Shivdasani

Android:管理多个通知

  •  9
  • Chander Shivdasani  · 技术社区  · 14 年前

    我正在尝试在应用程序中创建多个通知。为了唯一地标识每个通知,我为它们提供了唯一的标识ID。以下是我的代码:

    private void updateNotification(int notificationId, int clockStatusID, CharSequence text) {
     //notificationManager.cancel(notificationId);
    // throws up an ongoing notification that the timer is running
    Log.i("TIMERCOUNT", "Notification id: " + notificationId);
    Notification not = new Notification(clockStatusID, // the
        // icon
        // for
        // the
        // status
        // bar
        text, // the text to display in the ticker
        System.currentTimeMillis() // the timestamp for the
        // notification to appear
    );
    Intent intent = new Intent();
    intent.putExtra("notificationID", notificationId);
    intent.setAction("actionstring" + System.currentTimeMillis());
    intent.setClassName("com.chander.time.android.activities",
    "com.chander.time.android.activities.Tabs");
    
    
    not.setLatestEventInfo(self,
        getText(R.string.timer_notification_title),
        getText(R.string.timer_on_notification_text), PendingIntent
        .getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT));
    
    not.flags += Notification.FLAG_ONGOING_EVENT;
    not.flags += Notification.FLAG_NO_CLEAR;
    notificationManager.notify(notificationId, not);
    }
    

    问题: 选择通知后,将调用选项卡活动传递意图。我要访问在选项卡中选择的通知的唯一通知ID。我尝试了intent.putextra()来保存intent中的notificationID。但是,对于多个通知,它会覆盖notificationID并返回最新的通知。我不明白为什么会发生这种情况,我如何才能避免这种覆盖性的通知ID。

    谢谢, 钱德尔

    2 回复  |  直到 10 年前
        1
  •  15
  •   Chander Shivdasani    14 年前

    我得到了答案。他们的意图越来越深入人心。为了达到一个新的目的,只需添加以下代码:

    saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
    

    这使得意图独一无二。

    另外,有人建议我使用以下代码,以使意图独特:

    saveCallIntent.setAction("actionstring" + System.currentTimeMillis());
    

    这对我没有帮助,但可能对其他人有帮助。

    ——Chander

        2
  •  7
  •   Ken Y-N    10 年前

    只要放置 notificationId 而不是0英寸 pendingIntent .

    PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT));
    

    用户还必须将以下代码与上述代码一起更新,以使其正常工作。

     mNotificationManager.notify(notificationId, mBuilder.build());