0

I have an alarm set up to deliver an intent with a notification to a broadcast receiver which then fires the notification. Pre 7.0, the notification is present in the intent when received, on 7.0 it's missing.

Here's the code which generates the notification.

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

Here's the pending intent code.

public static PendingIntent generatePendingIntent(Context context, Intent notificationIntent) {
    return PendingIntent.getBroadcast(context, 0, notificationIntent,
                                      PendingIntent
                                              .FLAG_UPDATE_CURRENT);
}

Here's the code that receives it.

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

Any ideas? Thanks!

Zach Sperske
  • 658
  • 9
  • 28
  • What is the implementation of `generatePendingIntent()`? – CommonsWare May 11 '17 at 20:00
  • @CommonsWare just added. – Zach Sperske May 11 '17 at 21:09
  • OK, that seems fine. Have you tried adding another simpler extra to see if it comes through? IOW, is the problem with extras in general or with your relatively funky one? – CommonsWare May 11 '17 at 21:39
  • I noticed that a serialized enum that I sent along was missing as well, I know there were changes in 7.0 with regards to intent transactions that were too large, but I'm not sure how that would apply here as no exceptions were thrown. I opted to simply send along the required info to construct the notification in onReceive. – Zach Sperske May 15 '17 at 18:20
  • 1
    Just have a look on [this answer](https://stackoverflow.com/a/39574137/6161100) – P-Zenker Oct 04 '17 at 15:20
  • Nice, my objects were pretty trivial so using primitives to reconstruct was no problem but that answer is definitely the "better" (still not great) way. Thanks for the comment! – Zach Sperske Oct 05 '17 at 04:41

1 Answers1

0

I noticed that a serialized enum that I sent along was missing as well, I know there were changes in 7.0 with regards to intent transactions that were too large, but I'm not sure how that would apply here as no exceptions were thrown. I opted to simply send along the required info to construct the notification in onReceive.

Zach Sperske
  • 658
  • 9
  • 28