Android 如果我从自己那里得到一个新的通知,通知将被删除

Android 如果我从自己那里得到一个新的通知,通知将被删除,android,notifications,android-notifications,google-cloud-messaging,android-notification-bar,Android,Notifications,Android Notifications,Google Cloud Messaging,Android Notification Bar,我有一个下面的类,每当我收到推送通知时就会被调用。(尽管在任何地方都能工作)。 问题是,如果我从另一台设备向我的手机(比方说iphone)发送通知,我会收到通知,它会留在通知栏中。如果我不触摸它,并发送另一个,另一个将到达,通知栏中现在会出现两个图标,但是,当我从自己的手机向自己发送通知时,栏中所有当前的通知都会消失,并被我的通知取代。当我发送一个新图标时,不会显示其他图标,而是更新旧图标 如何使每个通知显示在通知栏中?(因为每个人都有一个唯一的ID,通过在指定活动开始时做特定事情的意图发送给它

我有一个下面的类,每当我收到推送通知时就会被调用。(尽管在任何地方都能工作)。 问题是,如果我从另一台设备向我的手机(比方说iphone)发送通知,我会收到通知,它会留在通知栏中。如果我不触摸它,并发送另一个,另一个将到达,通知栏中现在会出现两个图标,但是,当我从自己的手机向自己发送通知时,栏中所有当前的通知都会消失,并被我的通知取代。当我发送一个新图标时,不会显示其他图标,而是更新旧图标

如何使每个通知显示在通知栏中?(因为每个人都有一个唯一的ID,通过在指定活动开始时做特定事情的意图发送给它)


您用于显示通知的过程现在已折旧。还可以使用以下命令为
mNotificationManager.notify(uniqueId,mBuilder.build()
指定唯一的int来显示单独的通知

private void generateNotification(Context context, String message, String notificationId) {
    int notify = Integer.valueOf(notificationId);
    NotificationManager mNotificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent resultIntent = new Intent(context, ViewPost.class);
    Bundle params = new Bundle();
    params.putString("group_id", this.groupId.get(notificationId));
    params.putString("post_id", this.postId.get(notificationId));
    params.putString("notification", "notification");
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    resultIntent.putExtras(params);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(ViewPost.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(notify,
                PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("FleeGroups Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .setContentText(message)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager.notify(notify, mBuilder.build());
}
尝试将此用于id: int id=(int)System.currentTimeMillis()


可能这两个通知是一个接一个地发送的,/1000使其解析为相同的号码。

@Muhammadamirali,你确定它们已被弃用吗?你确定你的代码解决了我的问题吗?在我看来,它几乎是相同的代码。@Carnal唯一的区别是TaskStackTrace,tony只是传递了唯一的id到mNotificationManager.notify()方法。您的问题将是solved@MuhammadAamirALi我已经在这样做了,因此系统/1000@tony9099我不明白为什么你的代码应该删除所有其他通知。当然,你没有在某处调用cancelAll()或cancel(id)?@tony9099,如果你的“id”您传入的方法是唯一的,您可以尝试使用它然后看看这是否有效。尝试清理你的项目或其他东西,我有与你相同的代码,它现在正在为我工作:/does.same result:/非常不可预测的行为你有时会有多个通知,还是总是在顶部栏中只有1个通知。不,如果我的应用不可见,我肯定会收到多个通知。但如果它可见,事情就会发生错误和随机的事情发生了。。。
private void generateNotification(Context context, String message, String notificationId) {
    int notify = Integer.valueOf(notificationId);
    NotificationManager mNotificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent resultIntent = new Intent(context, ViewPost.class);
    Bundle params = new Bundle();
    params.putString("group_id", this.groupId.get(notificationId));
    params.putString("post_id", this.postId.get(notificationId));
    params.putString("notification", "notification");
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    resultIntent.putExtras(params);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(ViewPost.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(notify,
                PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("FleeGroups Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .setContentText(message)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager.notify(notify, mBuilder.build());
}