实际上我的应用程序有一个活动。为了创建通知,我必须传递待处理的活动意图。
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 不是一个活动。它是一个简单的类。 在这种情况下是否可以创建多个通知?以及如何? 提前谢谢!
答案 0 :(得分:8)
要获得单独的通知,您需要为每个通知使用不同的ID
。
这是一个简单的样本:
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:
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);
}