Ios 如果应用程序处于非活动状态,则访问推送有效负载
我有一个推送通知,当应用程序收到它时,我会调用以下命令Ios 如果应用程序处于非活动状态,则访问推送有效负载,ios,push,appdelegate,Ios,Push,Appdelegate,我有一个推送通知,当应用程序收到它时,我会调用以下命令 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { if userInfo["t"] as! String == "rqst" { print("type is help request") if let token = NSU
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
}
}
这将初始化故事板。但若我的应用程序被系统杀死,并且它关闭,设备接收到推送,在点击推送后,什么都不会发生
似乎我必须使用应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)
如果关闭应用程序
但是如何在didFinishLaunchingWithOptions中访问用户信息呢?这是在Objective C中,但在Swift中也是一样。将其放入
didfishlaunchingwithoptions
:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self application:application didReceiveRemoteNotification:remoteNotif];
}
斯威夫特:
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {...}
这在目标C中,但在Swift中也是一样。将其放入
didfishlaunchingwithoptions
:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self application:application didReceiveRemoteNotification:remoteNotif];
}
斯威夫特:
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {...}
您可以在didFinishLaunching中使用
UIApplicationLaunchActionsRemoteNotificationKey
作为启动选项进行检查
UIApplicationLaunchActionsRemoteNotificationKey:表示
应用程序可以处理远程通知。价值
此密钥是一个NSDictionary,其中包含远程服务器的有效负载
通知。>-警报:警报消息的字符串或
具有两个键的字典:body和show view.>-徽章:号码
指示要从提供程序下载的数据项的数量。
此号码将显示在应用程序图标上。没有徽章
属性表示当前标记图标的任何数字都应
被移除。>-声音:应用程序包中的声音文件的名称
播放警报声。如果指定了“默认”,则默认声音
应该玩
您可以在application:didfishlaunchwithoptions:
中手动调用application:didReceiveMotonification:
目标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
}
您可以在didFinishLaunching中使用
UIApplicationLaunchActionsRemoteNotificationKey
作为启动选项进行检查
UIApplicationLaunchActionsRemoteNotificationKey:表示
应用程序可以处理远程通知。价值
此密钥是一个NSDictionary,其中包含远程服务器的有效负载
通知。>-警报:警报消息的字符串或
具有两个键的字典:body和show view.>-徽章:号码
指示要从提供程序下载的数据项的数量。
此号码将显示在应用程序图标上。没有徽章
属性表示当前标记图标的任何数字都应
被移除。>-声音:应用程序包中的声音文件的名称
播放警报声。如果指定了“默认”,则默认声音
应该玩
您可以在application:didfishlaunchwithoptions:
中手动调用application:didReceiveMotonification:
目标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
}
尝试了代码,但如果打开推送,则不会发生任何事情。应用程序未打开应用程序未打开或未调用DidReceiveMemotentification?哦,不,抱歉,应用程序已打开,但随后立即崩溃,并更新了swift代码。请现在检查。Thanks@technerd,Objective-c代码缺少返回值。已尝试代码,但如果打开推送,则不会发生任何情况。应用程序未打开应用程序未打开或未调用DidReceiveMemotentification?哦,不,抱歉,应用程序已打开,但随后立即崩溃,并更新了swift代码。请现在检查。Thanks@technerd,Objective-c代码缺少返回值。在
DidReceiveMemoteNotify:remoteNotif
中,我使用“[RCTLinkingManager应用程序:应用程序openURL:url选项:@{};”但是它不起作用。在didReceiveMemotentification:remoteNotif
中,我使用“[RCTLinkingManager应用程序:应用程序openURL:url选项:@{};”但它不起作用。