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

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

问题描述

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

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 ?

推荐答案

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

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

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

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天全站免登陆