Android:在应用程序的设备中管理多个推送通知

标签 android notifications push-notification android-notifications android-notification-bar

我正在开发一个应用程序,我在其中实现了推送通知 功能。 我的 onMessage - GCMIntentService.java 代码是:

@Override
protected void onMessage(Context context, Intent data) {
    String message = data.getExtras().getString("message");

    displayMessage(context, message);
    generateNotification(context, message);
}

以及generateNotification的代码——

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, GCMMessageView.class);

    notificationIntent.putExtra("message", message);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

这段代码运行良好。我收到推送消息和通知。

=>但是当我发送多条消息时,通知会被覆盖。而且我只能在状态栏中看到最后一条通知。

应用程序的需要是-如果我发送多于一个通知,那么所有的通知都应该显示在状态栏中。

所以请指导我在我的代码中应该怎么做。

最佳答案

尝试在 intent 和 notify 中放置不同的通知 ID(即 nid)而不是每个新通知的 0,这将防止覆盖您的通知

  PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0);

notificationManager.notify(nid, notification);

希望对你有帮助

关于Android:在应用程序的设备中管理多个推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858315/

相关文章:

java - 下面带有滚动和编辑文本的弹出菜单

android - 如何在android中显示日历控件?

java - 在Android中创建基于表插入的通知

php - 未加载 WebPush gmp 扩展,但需要使用 payload 发送推送通知。您可以在 php.ini 中修复此问题

java - setSmallIcon() 和 setLargeIcon() 在 Android 通知中不起作用

android - VM 退出,结果代码为 0,清除已跳过

java - 无法按照我想要的方式拆分字符串

android - 如何使用位图作为通知图标

push-notification - Xamarin.Forms Azure 推送通知 - "MobileServiceInvalidOperationException: You do not have permission to view this directory or page"

ios - 运行时从任何地方的 didReceiveRemoteNotification presentViewController