我设置了一个警报,通过向广播接收器发送通知,然后发出通知。在7.0之前,通知在收到时出现在意图中,在7.0上它已经丢失。
这是生成通知的代码。
public static void scheduleNotification(Context context, String message, long delay,
MainDisplay.NotificationType type) {
Notification.Builder builder = new Notification.Builder(context).setSmallIcon(
R.drawable.ic_stat_o)
.setContentTitle(
"Title")
.setContentText(
message)
.setStyle(
new Notification
.BigTextStyle()
.bigText(
message));
final Intent notificationIntent = generateNotificationIntent(context);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, builder.build());
notificationIntent.putExtra(TYPE_KEY, type);
final PendingIntent pendingIntent = generatePendingIntent(context, notificationIntent);
final long futureInMillis = SystemClock.elapsedRealtime() + delay;
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
这是未决的意图代码。
public static PendingIntent generatePendingIntent(Context context, Intent notificationIntent) {
return PendingIntent.getBroadcast(context, 0, notificationIntent,
PendingIntent
.FLAG_UPDATE_CURRENT);
}
这是接收它的代码。
public void onReceive(final Context context, Intent intent) {
final NotificationManager
notificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
final Notification notification = intent.getParcelableExtra(NOTIFICATION);
final int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
有什么想法吗?谢谢!
答案 0 :(得分:0)
我注意到我发送的序列化枚举也丢失了,我知道7.0中有关于意图事务太大的变化,但是我不知道这样做会如何适用于抛出异常。我选择简单地发送所需信息以在onReceive中构建通知。