是否可以知道应用程序是否通过推送通知启动/打开?
我猜这个发布活动可以在这里找到:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当应用程序处于后台时,如何检测到它是从推送通知中打开的?
push-notification apple-push-notifications uiapplicationdelegate ios ios6
撕裂我的头发,以便在iOS10中使用推送通知.目前的设置:
在func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
:
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if error == nil {
print("DID REQUEST THE NOTIFICATION")
UIApplication.shared.registerForRemoteNotifications()
}
}
print("DID SET DELEGATE")
}
Run Code Online (Sandbox Code Playgroud)
在func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
:
print("DID REGISTER FOR A REMOTE NOTIFICATION AND THE TOKEN IS \(deviceToken.base64EncodedString())"
let request = UpdatePushNotificationSubscription_Request(deviceToken: deviceToken)
updatePushNotificationSubscriptionWorker.updateSubscription(request)
Run Code Online (Sandbox Code Playgroud)
我已经检查过令牌是否正确地上传到后端,它确实匹配.
我也实施了:
@available(iOS 10.0, *) …
Run Code Online (Sandbox Code Playgroud) 我知道有很多次问过类似的问题.但是在阅读这些主题之后,我仍然非常困惑,特别UNUserNotificationCenter
是在iOS 10中引入之后.
官方文档提到了我可以处理远程通知的3种方法:
userNotificationCenter:willPresentNotification:withCompletionHandler:
在应用程序处于前台时处理通知.userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
时,应用程序在后台或没有运行.application:didReceiveRemoteNotification:fetchCompletionHandler:
app委托的方法.所以,
并且,更令人困惑的是:如果应用程序处于后台,则何时调用委托方法:何时收到通知消息?或者当用户点击通知时?
我想知道是否有办法知道某个应用程序(可以在后台关闭或打开)是否已通过点击启动:
谢谢 !!