gpt4 book ai didi

ios - 如果应用程序处于非事件状态,则访问推送负载

转载 作者:行者123 更新时间:2023-11-29 10:21:30 25 4
gpt4 key购买 nike

我有一个推送通知,当应用收到它时,我调用以下方法

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

if userInfo["t"] as! String == "rqst" {

print("type is help request")

if let token = NSUserDefaults.standardUserDefaults().objectForKey("authToken") {
authTokenOfHelper = token as! String
}

let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("helperMap")
let navController = UINavigationController.init(rootViewController: viewController)
self.window?.rootViewController = nil
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()

helpRequestReceived = true

}

}

这会初始化 Storyboard。但是如果我的应用程序被系统杀死并且它已关闭并且设备收到推送,则在点击推送后什么也不会发生。

似乎我必须使用 application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 如果应用程序关闭

但是如何访问 didFinishLaunchingWithOptions 中的 userInfo 呢?

最佳答案

您可以使用 UIApplicationLaunchOptionsRemoteNotificationKey 作为启动选项在 didFinishLaunching 中进行检查。

UIApplicationLaunchOptionsRemoteNotificationKey: Indicates that a remote notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the remote notification. > - alert: Either a string for the alert message or a dictionary with two keys: body and show-view. > - badge: A number indicating the quantity of data items to download from the provider. This number is to be displayed on the app icon. The absence of a badge property indicates that any number currently badging the icon should be removed. > - sound: The name of a sound file in the app bundle to play as an alert sound. If “default” is specified, the default sound should be played.

您可以在 application:didFinishLaunchingWithOptions: 中手动调用 application:didReceiveRemoteNotification:

objective-c

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...

if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}

return YES;
}

swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

self.application(application, didReceiveRemoteNotification: launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey]! as! [NSObject : AnyObject])

}


return true
}

关于ios - 如果应用程序处于非事件状态,则访问推送负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34923452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com