一个活动的多个通知 [英] Multiple notifications for one activity

查看:108
本文介绍了一个活动的多个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我的应用程序有一个活动。对于创建通知我必须通过未决活动的意图。

Actually my application have one activity.For creating a notification i have to pass the pending activity intent.

NotificationManager mgr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note=new Notification(mob.app.R.drawable.message,"Message!",System.currentTimeMillis());
        // This pending intent will open after notification click
        PendingIntent i=PendingIntent.getActivity(this, 2,new Intent(this,SaleNotification.class),0);


        note.setLatestEventInfo(activity,messageHeading,message, i);

        //After uncomment this line you will see number of notification arrived
        note.number=notifyNumber;
        mgr.notify(0, note);

下面的 SaleNotification.class 不是一个activity.It是简单的类。 是否有可能创建通知多个在这种情况下?又如何? 在此先感谢!

Here SaleNotification.class is not an activity.It is simple class. Is it possible to create notification more than one in this case?and how? Thanks in advance!

推荐答案

要拥有你需要使用一个不同的 ID单独通知的每一个。

To have separate notifications you need to use a different ID for each one.

这是一个简单的示例:

private int SIMPLE_NOTFICATION_ID_A = 0;
private int SIMPLE_NOTFICATION_ID_B = 1;

然后

// display A
displayNotification("Extra for A", "This is A", "Some text for activity A", MyActivityA.class, SIMPLE_NOTFICATION_ID_A);
// display B
displayNotification("Extra for B", "This is B", "Some text for activity B", MyActivityB.class, SIMPLE_NOTFICATION_ID_B);

和displayNotification:

and displayNotification:

private void displayNotification(String extra, String contentTitle, String contentText, Class<?> cls, int id) {     

  Notification notifyDetails = new Notification(R.drawable.icon, "New Alert!", System.currentTimeMillis());
  Intent intent = new Intent(this, cls);
  intent.putExtra("extra", extra);
  PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_ONE_SHOT);
  notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
  mNotificationManager.notify(id, notifyDetails);
}

这篇关于一个活动的多个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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