Is there an equivalent solution like iOS localNotification on Android platform?

My app needs to show notifications to users at a specific time in the future (maybe months). On iOS, all I have 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, on Android, this is much more complicated than I expected:

  • I need to set up alarms with an alarm manager in order to start the service at any given time in the future.

  • These services, in turn, generate the notification and use the Notification Manager to route the notification to the status bar, etc.

But this solution has problems:

  • If the device reboots before the alarm is triggered, the alarm will be lost. I can register to download like this and reinstall the services. However, I really want to avoid setting duplicate alarms for the same event, but there seems to be no way to get the set alarms from the alarm manager?
  • The notification is made at a scheduled time in the future (say a month later), not when I set the notification (now). The required notification data may not be available at that time. I don't see any way to get around this other than keeping the relevant data and wait for the alarm to go off.

Is there a painless solution to the local notification problem on Android?

+3


source to share


1 answer


If the device reboots before the alarm is triggered, the alarm will be lost. I can register to download like this and reinstall the Services. However, I really want to avoid setting duplicate alarms for the same event, but there doesn't seem to be a way to get the currently set alarms from the alarm manager?

How are you going to receive duplicate signals when you restart your device? When the device restarts, all alarms are canceled, so you will do as you said where you start it again at BroadcastReceiver

when the device boots

The notification is made at a scheduled time in the future (say a month later), not when I set the notification (now). The required notification data may not be available at that time. I don't see any way around this other than storing the relevant data and waiting for the alarm to go off.

right, you need to save the data you want to show in the notification SharedPreferences

will probably be the easiest

EDIT:



You don't need to keep a reference to the pending intent, all you need to do is create the intent in the same way.

when you first created the alarm you used this intent

Intent intent = new Intent(context,MyClass.class);
    //any flags or extras

PendingIntent.getBroadcast(context, 0, intent, 0);

      

Now you want to cancel this signal, so just create the same intent again, it should be 1 for 1 original

Intent intent = new Intent(context,MyClass.class);
    //any flags or extras that you previously had

PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr.cancel(pi);

      

+6


source







All Articles