Rya*_*zzo 5 ios firebase swift firebase-realtime-database firebase-cloud-messaging
我的项目使用Firebase Notifications作为其APNs服务,但我一直使用Firebase控制台作为测试向我的设备发送通知,并且它们仅在前台显示(通过控制台输出).当应用程序在后台或设备处于锁定屏幕时,设备不会收到任何通知.但是,当我打开应用程序备份时,控制台输出最终会从applicationReceivedRemoteMessage方法到达.
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print("%@", remoteMessage.appData)
print("QQQQQ")
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
%@ [AnyHashable("notification"): {
body = Hi;
e = 1;
}, AnyHashable("from"): 492525072004, AnyHashable("collapse_key"): org.myApp]
QQQQQ
Run Code Online (Sandbox Code Playgroud)
iOS 和数据消息存在问题。它在这里指出
在 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"
}
}
Run Code Online (Sandbox Code Playgroud)
2)用户打开应用程序时触发的数据通知
{
"to" : "/topics/yourTopicName",
"data" : {
"yourData" : "1",
"someMoreOfYourData" : "This is somehow the only workaround I've come up with."
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在该- (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.
}
Run Code Online (Sandbox Code Playgroud)
我还将保留此代码来触发应用程序内的通知(创建本地通知),因为您可以使用它来创建静音横幅,也许这样,即使应用程序进入前台,用户也会再次收到通知:
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];
Run Code Online (Sandbox Code Playgroud)
它将在应用程序进入前台的同一秒触发通知。
| 归档时间: |
|
| 查看次数: |
1814 次 |
| 最近记录: |