My app needs to show notifications to users at certain specified time in the future (maybe months away). In iOS, all I need to do is this:
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
However, in Android, it is much more complicated than I originally expected:
I have to install alarms with Alarm Manager, to start a service at each specified time in future.
These services, in turn, create a notification, and use Notification Manager to push the notification to status bar, etc.
But this solution has problems:
- If the device is restarted before the alarm fires, the alarm is lost. I can register for boot-up broadcast like this and re-install the services. However, I really want to avoid installing duplicate alarms for the same event, but it seems there is no way to get currently installed alarms from the Alarm Manager?
- The notification is composed at the scheduled time in future (say a month later), not when I set the notification(now). At that time, the required data for the notification may no longer be available. I don't see any way around this, except to store the relevant data, and wait for the alarm to fire.
Is there a pain-free solution to the local notification problem on Android?