我使用了广播接收器和警报管理器。在特定日期和时间添加了通知。 通知显示正常。但是当用户 触摸通知 n 时,我想 启动myApplication 。
Notification.Builder notification= new Notification.Builder(this);
notification.setContentTitle("MY Title");
notification.setContentText("Today you have scheduled for...");
notification.setSmallIcon(R.drawable.ic_app_launcher);
notification.setAutoCancel(true);
notification.build();
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = dateSpecified.getTime(); //Some future date like 20 feb 2015
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
NotificationPublisher类-BroadcastReciver
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
我提到了link。
我希望你理解我想说的话。
请有人帮助我。非常感谢。
答案 0 :(得分:0)
但是当用户触摸我要发布的通知时 所有MyApplication。
因为NotificationPublisher
BroadcastReceiver会在点击通知时触发,因此从Receiver的onReceive
方法启动应用程序:
public void onReceive(Context context, Intent intent) {
// start application here
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentn = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intentn);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);
}