[英]How to display FCM 'data' message received using firebase-cpp-sdk on iOS?
I need to display a notification when my app is in background or killed.当我的应用程序处于后台或被终止时,我需要显示通知。 I have no problem to display FCM 'notification' message on iOS using firebase-cpp-sdk, I tried quickstart-cpp/messaging/testapp
and it just worked.我使用 firebase-cpp-sdk 在 iOS 上显示 FCM '通知'消息没有问题,我尝试quickstart-cpp/messaging/testapp
并且它工作正常。 But when 'data' message is received when app is in background or foreground - no notification is displayed, I just see it in the log that message is received.但是,当应用程序处于后台或前台时收到“数据”消息时 - 没有显示通知,我只是在日志中看到收到消息。
I use "content_available": true
in message as suggested by many answers.正如许多答案所建议的那样,我在消息中使用"content_available": true
。 This helps to actually receive the 'data' message, as I can see in the log, but the message is not displayed.正如我在日志中看到的那样,这有助于实际接收“数据”消息,但未显示该消息。 I tried legacy HTTP and HTTP v1 protocols, the result is the same.我尝试了旧版 HTTP 和 HTTP v1 协议,结果是一样的。 An example of legacy HTTP message is:传统 HTTP 消息的示例是:
{
"data": {
"body": "Entrance door Intrusion at 2 Jun 2020 01:32:08",
"title": "Intrusion"
},
"content_available": true,
"to": "fcm_device_token_here"
}
Do I need to manually create a notification like on Android?我是否需要像 Android 一样手动创建通知? Or there are some other ways to do it?或者还有其他一些方法可以做到吗?
To asnwer my own question - yes, I created a local notification.回答我自己的问题 - 是的,我创建了一个本地通知。 For that I used qt-notification library since I am using QT, but the code example to show notification is the following (taken from the project):为此,我使用了qt-notification库,因为我使用的是 QT,但显示通知的代码示例如下(取自项目):
// create content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = title.toNSString(); // your title
content.body = caption.toNSString(); // your notification text
content.sound = !sound.isEmpty() ? [UNNotificationSound soundNamed: sound.toNSString()] : [UNNotificationSound defaultSound]; // your sound
// content.sound = [UNNotificationSound criticalSoundNamed: sound.toNSString() withAudioVolume: 1.0]; // For that you need Apple's permission
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// create trigger time
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
// unique identifier
NSString* identifierNSString = identifier.toNSString();
// create notification request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifierNSString
content:content trigger:trigger];
// add request
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = id(m_Delegate);
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Local Notification failed");
}
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.