繁体   English   中英

Xamarin.forms 安排本地通知

[英]Xamarin.forms Schedule Local notifications

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM