【问题标题】:I need my android app to show multiple notifications我需要我的 android 应用程序来显示多个通知
【发布时间】:2015-09-22 12:40:07
【问题描述】:

我在互联网上没有找到解决问题的单一解决方案。

    int notificationId = Integer.parseInt(Common.getStringSharedPreference(context, "NOTIFICATION_ID", "ID", "1"));

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.app_logo, message, System.currentTimeMillis());
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = null;
    notificationIntent = new Intent(context, MyLawyerActivity.class);
    notificationIntent.putExtra(ExtrasKeys.ITEM_OBJECT, (Serializable) object);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, notificationId, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.sound = Settings.System.DEFAULT_NOTIFICATION_URI;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

    Common.putStringSharedPreference(context, "NOTIFICATION_ID", "ID", ""+(notificationId+1));

请注意,我正在使用带有棒棒糖的设备进行测试。

【问题讨论】:

  • 你应该为每个通知使用不同的 id ...... notificationManager.notify( HERE , notification);
  • 谢谢@koutuk,他在您的原始评论前 5 秒提供了答案 :)

标签: android google-cloud-messaging android-notifications android-pendingintent


【解决方案1】:

只需更改多个通知的编号,例如

notificationManager.notify(0, notification);
notificationManager.notify(1, notification);

【讨论】:

  • 所以 id 应该去哪里!而不是这里 PendingIntent.getActivity(context, notificationId, notificationIntent, 0);那么我设置的这个是什么?
  • notificationManger.notify 方法的第一个参数是应用程序中通知的唯一 ID,第二个参数是我们使用 builder 方法创建的通知对象。
  • 经过测试和工作:) 我会在 5 分钟内接受答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
相关资源
最近更新 更多