我有用于创建通知的代码:
NotificationManager notifyMngr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(mId, "New alert!", System.currentTimeMillis());
Intent alert = new Intent(this, AlertInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, alert, 0);
notification.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent);
notifyMngr.notify((int) System.currentTimeMillis(), notification);
我需要创建一些通知,每个通知都必须通过单击执行一个新的AlertInfoActivity对象。但这段代码总是执行 1 个 Activity 对象。我该如何完成我的任务?
最佳答案
您需要为要启动的每个 Activity 创建一个通知。像这样:
NotificationManager notifyMngr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//First Notification
Notification notification=new Notification(mId, "New alert!", System.currentTimeMillis());
Intent alert = new Intent(this, AlertInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, alert, 0);
notification.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent);
notifyMngr.notify((int) System.currentTimeMillis(), notification);
//Second Notification
Notification notification2 = new Notification(mId, "Second Alert!", System.currentTimeMillis());
Intent alert2 = new Intent(this, DifferntAcitivty.class);
PendingIntent contentIntent2 = PendingIntent.getActivity(this, 0, notification2, 0);
notification2.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent2);
notifyMngr.notify((int) System.currentTimeMillis(), notification2);
//Third Notification
Notification notification3=new Notification(mId, "Third Alert!", System.currentTimeMillis());
Intent alert3 = new Intent(this, ThirdActivity.class);
PendingIntent contentIntent3 = PendingIntent.getActivity(this, 0, alert3, 0);
notification3.setLatestEventInfo(this, data.get("name"), data.get("post_date"), contentIntent3);
notifyMngr.notify((int) System.currentTimeMillis(), notification3);
//And so on and so forth.
关于java - 如何为每个通知创建一个新的 Activity 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7868132/