【发布时间】:2017-04-04 18:32:21
【问题描述】:
有没有办法在不启动应用程序的情况下拦截用户点击推送通知或用户点击丰富的推送通知操作按钮?我已经实现了所有必要的设置来注册推送通知、接收推送通知和处理简单而丰富的推送通知,以及用户点击丰富推送通知中的某个操作按钮。但是,当用户单击某个操作按钮时,应用程序会启动,我可以在内部处理它:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notification = JSONParser.parseRichPushNotification(notification: response.notification.request.content.userInfo)
if let responseIdentifier =
{
print(response.actionIdentifier)
}
//......
}
我想执行一些后台操作,例如不启动应用程序,或者如果推送通知数据中有链接,则打开网络浏览器。在 iOS 中可以吗?
我发现了类似的问题:
- Is there a way to open urls received in a push notification without opening the app in ios 10?
- Handle Push Notification when the App is off, or without opening the App at all - iOS
- Android notifications actions without opening the app
但我认为我没有找到我的问题的答案。我还阅读了有关静默通知的信息,但我不确定是否可以将它们用于此目的,即当用户单击操作按钮而不自动启动应用程序时执行某些操作。
顺便说一句,我正在使用 OneSignal 推送通知
有人知道如何执行此操作吗?提前致谢
【问题讨论】:
-
您在创建
UNNotificationAction对象时是否使用了foreground选项?你不应该。如果您不使用foreground,您的应用将被允许在不进入前台的情况下处理该操作 -
我使用的不是本地通知,而是远程通知。我通过推送通知接收操作按钮,所以我认为我不需要那个。我应该这样做吗?
-
如果用户点击通知然后你的应用程序将被启动。而已。如果您的应用是通过通知启动的,那么您可以在应用打开后立即打开一个 URL,但您的应用将首先出现。
-
@LouFranco 谢谢。将选项从 UNNotificationActionOptionForeground 更改为 UNNotificationActionOptionNone 对我有用!
标签: ios swift push-notification apple-push-notifications richpush