Problems with the real-time AlarmManager + Notifications

SoftwareKnightsSoftwareKnights ESMember
edited April 2016 in Xamarin.Android
  • When the app starts: I cancel all notifications, schedule renews and saves the current time in SharedPrefences.
  • When the device is reboot: I use the time saved in SharedPreferences an reschedule the notifications.

In this example there are two unique notifications.
The first is +2 minutes and the second notifcation + 4 minutes.
During testing, notifications are not displayed at the right time ...

To schedule notifications I use:
alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime () + (2 * 60 * 1000), pendingIntent); // +2min.
alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime () + (4 * 60 * 1000), pendingIntent); // +4min.

SplashScreen.cs
// Schedule notifications
var prefs = Application.Context.GetSharedPreferences("qmiran", FileCreationMode.Private);
var prefEditor = prefs.Edit();
prefEditor.PutInt("notificationTime", ((int) Java.Lang.JavaSystem.CurrentTimeMillis() / 1000));
prefEditor.Commit();

    // Cancel All Noti Alarms
    AlarmManager alarmManager = (AlarmManager) this.GetSystemService(Context.AlarmService);
    // Cancel Alarm id = 1 - 1month
    Intent intent = new Intent(this, typeof(TimerNotification));
    PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 1, intent, PendingIntentFlags.UpdateCurrent);
    alarmManager.Cancel(pendingIntent);
    // Cancel Alarm id = 2 - 3months
    pendingIntent = PendingIntent.GetBroadcast(this, 2, intent, PendingIntentFlags.UpdateCurrent);
    alarmManager.Cancel(pendingIntent);

    // Start Alarm id = 1 - 1 month notification
    Intent alarmIntent = new Intent(this, typeof(TimerNotification));
    alarmIntent.PutExtra ("alarmId", 1);
    alarmIntent.PutExtra ("title", "1mes" + GetString(Resource.String.notification_1month_title));
    alarmIntent.PutExtra ("message", GetString(Resource.String.notification_1month_message));
    pendingIntent = PendingIntent.GetBroadcast(this, 1, alarmIntent, PendingIntentFlags.UpdateCurrent);
    alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime () + (2 * 60 * 1000), pendingIntent); // 30 * 24 * 60 * 60 * 1000

    // Start Alarm id = 2 - 3 months notifications
    alarmIntent.PutExtra ("alarmId", 2);
    alarmIntent.PutExtra ("title", "2mes" +  GetString(Resource.String.notification_1month_title));
    alarmIntent.PutExtra ("message", GetString(Resource.String.notification_1month_message));
    pendingIntent = PendingIntent.GetBroadcast(this, 2, alarmIntent, PendingIntentFlags.UpdateCurrent);
    alarmManager.Set(AlarmType.ElapsedRealtimeWakeup , SystemClock.ElapsedRealtime () + (4 * 60 * 1000), pendingIntent); // 90 * 24 * 60 * 60 * 1000

**TimerNotification.cs
**It is a simple BroadcastReceiver with PowerManager.WakeLock and this creates and launches the notifications.
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
PowerManager.WakeLock w1 = pm.NewWakeLock (WakeLockFlags.Partial, "TimerNotification");
w1.Acquire();

    int id = intent.GetIntExtra("id", 0);
    var title = intent.GetStringExtra("title");
    var message = intent.GetStringExtra("message");

    Intent intentNoti = new Intent (context, typeof(SplashScreen));
    PendingIntent pendingIntent = PendingIntent.GetActivity (context, id, intentNoti, PendingIntentFlags.OneShot);

    // Instantiate the builder and set notification elements, including pending intent:
    Notification.Builder builder = new Notification.Builder (context)
    .SetContentIntent (pendingIntent)
    .SetContentTitle (title)
    .SetContentText (message)
    .SetSmallIcon (Resource.Drawable.Icon)
    .SetAutoCancel (true)
    .SetDefaults (NotificationDefaults.Sound | NotificationDefaults.Vibrate);

    Notification notification = builder.Build();
    NotificationManager notificationManager = context.GetSystemService (Context.NotificationService) as NotificationManager;
    notificationManager.Notify (id, notification);

    w1.Release ();

And finally** Startup.css **another BroadCastReceiver with BOOT_COMPLETED.
It obtains the initial date of SharedPreferences to subtract from current date and reschedule notifications.

Sign In or Register to comment.