确保在正确的时间 ios 交付 UILocalNotification [英] Assure Delivery Of UILocalNotification on correct time ios

查看:32
本文介绍了确保在正确的时间 ios 交付 UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的本​​地通知有问题:如果设备处于关闭状态或更改电话日期,通知将保留排队并与下一个通知一起触发.为什么?

I'm having a problem with my local notification that is: if the device is in off state or when changing the date of phone, the notification is kept in queue and firing along with next notification. Why?

由于需要非常关注通知日期..如何确保正确传递通知?

Since need to have very concern about the date of notification.. How to assure correct delivery of notification?

我们应该把删除过期通知的代码放在哪里?

Where should we put code for removing expired notification?

推荐答案

所以你的意思是你有一个名为 ABC 的通知将在 30 分钟内触发......但是你关闭了你的 iPhone 3 小时......然后打开它打开并看到通知 ABC 通知,即使它的时间已经过去?AFAIK ....通知仍然存在除非您使用removeDeliveredNotifications(withIdentifiers:)将其删除.

So you mean you had a notification named ABC to be triggered in 30 minutes...but then you turned your iPhone off for 3 hours...then turned it on and saw that notification ABC notification even though its time was passed? AFAIK ....the notification will still be there unless you remove it using removeDeliveredNotifications(withIdentifiers:).

基本上调用:

UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).

有关详细信息,请参阅此时刻WWDC 视频.

For more information see this moment of this WWDC video.

另请参阅此教程 查找4. 管理通知"部分

Also see this tutorial look for the section "4. Managing Notifications"

关于您的

老实说,我不确定……但我认为您应该这样做:

To be honest I'm not sure...but here's what I think you should do:

我建议创建创建通知的类:

I suggest to make the class that creates the notifications:

第一步:符合UNUserNotificationCenterDelegate

步骤 2: 实施:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
 withCompletionHandler completionHandler: @escaping () -> Void) {
}

现在你打开了你的设备......如果应用程序在后台,这个函数将被调用.您有机会清除过时的通知.

Now that you turned your device on...This function would get called only if app is in background. There you have a chance to kill your obsolete notifications.

这个函数在前台和后台都被调用,如果是后台,那么你可以在 step3 之后杀死过时的通知,如果它是前台,那么你不需要做任何事情,它自己发送队列,因为它被显示了!

This function is called in both foreground and background, if it's background then you can kill obsolete notifications following step3, if it's foreground well then you don't need to do anything, it send off the queue itself, because it was shown!

第三步:

内部您实现的功能:

UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in
            print("\(deliveredNotifications.count) Delivered notifications-------")
            for notification in deliveredNotifications{
               if (someCondition){
             UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).

              }
            }            
        })

<小时>

至于 iPhone 时间更改问题...您应该提出一个合理的条件并找到通知并将其删除...或者只需将您的通知触发器从 UNCalendarNotificationTrigger 更改为 UNTimeIntervalNotificationTrigger.(不过我不确定您的计时器是否会自行重置.我认为它不应该并且您很好).


As for the iPhone time change issue... you should come up with a reasonable condition and find the notification and remove it...or simply change your notification trigger from a UNCalendarNotificationTrigger to UNTimeIntervalNotificationTrigger. (though again I'm not sure if your timer resets itself or not. I think it shouldn't and you're good).

这篇关于确保在正确的时间 ios 交付 UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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