用户单击通知警报并且应用程序未运行时如何接收数据消息

时间:2017-04-27 09:12:11

标签: ios firebase firebase-cloud-messaging

在IOS下,当应用程序未运行时,我通过firebase clound messaging 发送消息并通知+数据有效负载,然后在ios设备上发送通知警报将向最终用户显示。但是,当用户点击它时,如何检索通知的数据有效负载?因为应用程序将启动,但应用程序中不会触发任何事件(即:未触发DidReceiveRemoteNotification)

此外,当应用程序在后台时,通知警报也会显示给最终用户。但是,当用户单击它时,将使用数据有效负载触发事件DidReceiveRemoteNotification

2 个答案:

答案 0 :(得分:1)

当应用关闭时,通知的内容将以您的Appdelegate方法didFinishLaunchingWithOptions作为launchOptions参数传递。

如果发生这种情况,你可以做的就是调用你的DidReceiveRemoteNotification方法,例如像这样(Objective-C):

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

// check if a remote notification was received while the app was closed, then take the necessary actions
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
    [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}

}

答案 1 :(得分:1)

在didFinishLaunchingWithOptions(AppDelegate)中:

 // Do what you want to happen when a remote notification is tapped.
            if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
                let userInfo: NSDictionary = launchOptions![UIApplicationLaunchOptionsKey.remoteNotification] as! NSDictionary
    }