Xamarin.forms 安排本地通知 [英] Xamarin.forms Schedule Local notifications

查看:34
本文介绍了Xamarin.forms 安排本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 https 中的文档://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications 关于本地通知.我实施它并且工作得很好.现在我的场景是:用户输入 Start DateRepetition timeNumber of Repetitions .我需要一些后台服务来调用这个推送通知?有什么建议可以在设备中安排这个吗?

I follow the documentation from https://docs.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?

更新我从这个链接添加了共享服务:https://www.c-sharpcorner.com/article/how-to-send-local-notification-with-repeat-interval-in-xamarin-forms/现在我不知道如何停止 Alarm Manager UILocalNotification 当我发送示例用户输入以发送 20 个通知,20 个通知后必须停止.

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.

推荐答案

我们可以使用 AlarmManager.CancelUIApplication.SharedApplication.CancelLocalNotification 来停止调度本地通知.

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);
        }
    }
}

安卓:

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");  
    }  
}

这篇关于Xamarin.forms 安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆