如果应用程序处于非活动状态,则访问推送有效负载 [英] Accessing push payload if app is inactive

查看:18
本文介绍了如果应用程序处于非活动状态,则访问推送有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a push notification, and when app receives it, I call the following

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

    }

}

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

this initialises storyboard.But if my app was killed by system and it is off and device recieves push, after tapping on push nothing is happened.

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

Seems that I have to use application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) if app is switched off

但是如何在 didFinishLaunchingWithOptions 中访问 userInfo ?

But how to access userInfo in didFinishLaunchingWithOptions ?

推荐答案

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

You can check this in didFinishLaunching using UIApplicationLaunchOptionsRemoteNotificationKey as launch options.

UIApplicationLaunchOptionsRemoteNotificationKey:表示一个远程通知可供应用程序处理.的价值这个键是一个包含远程有效载荷的 NSDictionary通知.> - 警报:警报消息的字符串或字典有两个键:body 和 show-view.> - 徽章:一个数字指示要从提供者下载的数据项的数量.这个数字将显示在应用程序图标上.没有徽章属性表示当前标记图标的任何数字都应该被移除.> - 声音:应用程序包中声音文件的名称作为警报声音播放.如果指定了default",则默认声音应该播放.

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:.

目标 C

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

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

   return YES;
}

迅捷

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
    }

这篇关于如果应用程序处于非活动状态,则访问推送有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆