如果应用程序处于非活动状态,则访问推送负载
Accessing push payload if app is inactive
我有一个推送通知,当应用程序收到它时,我调用以下命令
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.But,点击推送后没有任何反应。
如果关闭应用程序,我似乎必须使用 application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
但是如何访问 didFinishLaunchingWithOptions 中的 userInfo 呢?
这在 Objective C 中,但同样的事情在 Swift 中。把这个放在 didFinishLaunchingWithOptions
:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self application:application didReceiveRemoteNotification:remoteNotif];
}
Swift:
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {...}
您可以使用 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
}
我有一个推送通知,当应用程序收到它时,我调用以下命令
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.But,点击推送后没有任何反应。
如果关闭应用程序,我似乎必须使用 application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
但是如何访问 didFinishLaunchingWithOptions 中的 userInfo 呢?
这在 Objective C 中,但同样的事情在 Swift 中。把这个放在 didFinishLaunchingWithOptions
:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self application:application didReceiveRemoteNotification:remoteNotif];
}
Swift:
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {...}
您可以使用 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
}