iOS 和数据消息有问题。它声明here
在 iOS 上,FCM 存储消息并仅在应用程序处于
前台并已建立 FCM 连接。
所以必须有一个解决方法。和我类似的东西:
发送 2 条推送通知:
1) 使用此代码在应用程序处于后台时唤醒用户电话/启动正常:
{
"to" : "/topics/yourTopicName",
"notification" : {
"priority" : "Normal",
"body" : "Notification Body like: Hey! There something new in the app!",
"title" : "Your App Title (for example)",
"sound" : "Default",
"icon" : "thisIsOptional"
}
}
2) 用户打开应用时触发的数据通知
{
"to" : "/topics/yourTopicName",
"data" : {
"yourData" : "1",
"someMoreOfYourData" : "This is somehow the only workaround I've come up with."
}
}
因此,在- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage 方法下处理您的数据:
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", remoteMessage.appData);
//
//*** ABOUT remoteMessage.appData ***//
// remoteMessage.appData is a Key:Value dictionary
// (data you sent with second/data notification)
// so it's up to you what will it be and how will the
// app respond when it comes to foreground.
}
我还会留下这段代码来触发应用程序内部的通知(创建本地通知),因为您可以使用它来创建一个静音横幅,所以即使应用程序进入前台,用户也会再次收到通知:
NSDictionary *userInfo = remoteMessage.appData;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = userInfo[@"yourBodyKey"];
localNotification.alertTitle = userInfo[@"yourTitleKey"];
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
它会在应用进入前台的同一秒触发通知。