RazrMan
RazrMan

Reputation: 163

Xamarin.forms Schedule Local notifications

I follow the documentation from https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications about local notifications. I implement it and work perfectly fine. Now my scenario is : user enter Start Date , Repetition time and Number of Repetitions . I need some maybe background service to call this push notifications? Any suggestion maybe to schedule this in the device?

UPDATE I added shared service from this link : https://www.c-sharpcorner.com/article/how-to-send-local-notification-with-repeat-interval-in-xamarin-forms/ And now i don't know how to stop the Alarm Manager and UILocalNotification when i send example user entered to send 20 notifications , after 20 notification must stop.

Upvotes: 1

Views: 2080

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

We can use AlarmManager.Cancel and UIApplication.SharedApplication.CancelLocalNotification to stop the schedule local notifications .

iOS :

void CancleScheduleNotification()
{
    UILocalNotification[] localNotifications= UIApplication.SharedApplication.ScheduledLocalNotifications;
    //Traverse this array to get the UILocalNotification we want according to the key
    foreach (var localNotification in localNotifications)
    {
        if(localNotification.UserInfo.ObjectForKey(new NSString("key")).ToString() == "value")
        {
            UIApplication.SharedApplication.CancelLocalNotification(localNotification);
        }
    }
}

Android :

void CancleScheduleNotification()
{
    AlarmManager am = (AlarmManager)GetSystemService(Context.AlarmService);
    Intent intent = new Intent("LILY_TEST_INTENT");
    intent.SetClass(this, LilyReceiver.class);  
    intent.SetData(Android.Net.Uri.Parse("content://calendar/calendar_alerts/1"));  

    PendingIntent sender = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.NoCreate);  
    if (sender != null){  
        Log.Info("lily","cancel alarm");  
        am.Cancel(sender);  
    }else{  
        Log.Info("lily","sender == null");  
    }  
}

Upvotes: 5

Related Questions