Android:在应用程序的设备中管理多个推送通知 [英] Android: Manage multiple push notification in device of an app

本文介绍了Android:在应用程序的设备中管理多个推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an application in which i implemented Push notification functionality. My code of onMessage - GCMIntentService.java is:

@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);
}

此代码运行正常.我收到推送消息和通知.

This code is working fine. I am getting push message and notification.

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

应用程序的需求是-如果我发送然后一个通知,则所有通知应显示在状态栏中.

The need of application is that - if i send more then one notification then all should be display in status bar.

因此,请指导我在我的代码中应该怎么做.

So please guide me what should i do for that in my code.

推荐答案

尝试放入不同的通知ID(即nid),并为每个新通知通知而不是0,这将防止覆盖您的通知

Try to put different notification id (i.e. nid) in intent and notify instead of 0 for each new notification this will prevent over writing your notification

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

notificationManager.notify(nid, notification);

希望这对您有帮助

这篇关于Android:在应用程序的设备中管理多个推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆