gpt4 book ai didi

android - 一项 Activity 的多个通知

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:17 25 4
gpt4 key购买 nike

实际上我的应用程序有一个 Activity 。为了创建通知,我必须传递挂起的 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 。它是一个简单的类。在这种情况下是否可以创建多个通知?如何创建?提前致谢!

最佳答案

要获得单独的通知,您需要为每个通知使用不同的 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);

和显示通知:

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

关于android - 一项 Activity 的多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com