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