当应用程序在后台处理本地推送通知 [英] handling local push notifications when app is in the background

查看:86
本文介绍了当应用程序在后台处理本地推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 10中,我看到有两个用于处理本地推送通知的功能:

In iOS 10, I see that there are two functions for handling local push notifications:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

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

当应用程序在前台时发生推送通知时,将调用第一个.第二种情况是当应用程序在后台且用户单击通知时发生推送通知.

The first is invoked when a push notification happens when the app is in the foreground. The second occurs when a push notification happens when the app is in the background AND the user clicks on the notification.

我的问题是,当应用程序在后台运行时,是否有一种方法可以处理本地推送通知,而无需用户点击它?理想情况下,无论应用程序是在后台还是在前台,我都希望所有推送通知都可以触发CloudKit更新.这有可能吗?

My question is, is there a way to handle a local push notification when the app is in the background WITHOUT the user having to click on it? Ideally, I'd like all of my push notifications, whether while the app is in the background or foreground, to trigger a CloudKit update. Is this even possible?

谢谢!

推荐答案

我能想到的唯一方法是向您的应用程序添加一个Notifications Extension.有两种类型:"通知服务扩展"和"通知内容扩展".

The only way I can think of is adding a Notifications Extension to your app. There are two types: "Notification Service Extension" and "Notification Content Extension".

这两种方法原本都是用于其他目的,但是两者都可以在这种情况下工作,因为它们都可以实现方法 didReceive(_:withContentHandler:),并且应用程序在后台运行,则取决于扩展程序是否可以接收通知并对通知做出反应.

Both are originally meant for something else, however both could work for this scenario, since both of them can implement the method didReceive(_:withContentHandler:), and if the app is in the background, it's up to the extension to receive and react to the notification.

我建议使用" Notification Service Extension "(其原本目的是在显示通知之前更改通知的内容,但您可以不修改它),并实现 didReceive(_:withContentHandler:)方法.只需使用相同的原始通知调用内容处理程序,然后触发CloudKit更新.

I recommend using a "Notification Service Extension" (it's originally meant for changing the notification's content right before displaying it, but you could just leave it unmodified), and implementing the didReceive(_:withContentHandler:) method. Simply invoke the content handler with the same original notification and also trigger the CloudKit update.

关于如何添加服务扩展,这是一个很好的教程:

Here's a good tutorial on how to add the service extension: https://code.tutsplus.com/tutorials/ios-10-notification-service-extensions--cms-27550

在其"3.扩展代码"部分中,有一个有关如何实现该方法的示例,并带有注释"//编辑副本的属性",这正是我要添加该代码的地方.代码以触发CloudKit更新,因此只要在后台运行该应用程序,便会在显示通知之前立即执行.

In its section "3. Extension Code" there's an example of how to implement the method, with a comment that says "// Edit properties of copy", that's exactly where I would add the code to trigger the CloudKit update, so it's executed right before showing the notification whenever the app is on the background.

这篇关于当应用程序在后台处理本地推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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