0

I am trying to schedule a future notification using AlarmManager from FirebaseMessagingService when onMessageReceived().

Note: onMessageReceived() gets triggered, and the same code works within an activity, but not within the service.

private void setReminder() {
    Intent notificationIntent = new Intent(this, ReminderReceiver.class);
    PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}

Thanks!

arblitroshani
  • 13
  • 1
  • 5
  • take a look at this see if this helps you https://stackoverflow.com/a/50386399/9025311 –  May 20 '18 at 10:46
  • The notification shows correctly when calling the function from an activity, but not from the service – arblitroshani May 20 '18 at 19:44
  • what do you mean not from the service –  May 20 '18 at 19:51
  • Same code (the method), when called from a running activity gives the expected result. When called within the `onMessageReceived()` of `FirebaseMessagingService`, it doesn't work. – arblitroshani May 20 '18 at 20:30
  • This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification. –  May 20 '18 at 20:32
  • another example https://stackoverflow.com/a/37471326/9025311 –  May 20 '18 at 20:34
  • Thanks for the other link! My use case is this: - I send a notification with Firebase Functions on a certain trigger. - The notification gets displayed correctly. - After showing the notification, I want to schedule another one for a future time with AlarmManager – arblitroshani May 20 '18 at 20:41
  • then you need to use the other link above –  May 20 '18 at 20:43
  • 1
    I appreciate your help! – arblitroshani May 20 '18 at 20:45

0 Answers0