简体   繁体   English

如何在 iOS 上显示使用 firebase-cpp-sdk 收到的 FCM“数据”消息?

[英]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.

相关问题 应用程序在后台时未收到 Swift FCM 数据消息 (iOS 10) - Swift FCM Data Message not received when App is in background (iOS 10) 使用Phonegap插件在IOS Cordova上收到Firebase FCM通知 - Firebase FCM notification to received on IOS cordova using the Phonegap Plugin 如何使用 FCM 在 iOS 和 Android 中跟踪收到的通知? - How to track received notification in iOS and Android using FCM? Flutter - Firebase 云消息,在 iOS 上未收到数据消息 - Flutter - Firebase Cloud Messaging, Data Message is not received on iOS iOS FCM未接收到数据消息 - IOS FCM unreceived data message 如何从 iOS 中的 Firebase FCM 推送通知获取数据 - How to get data from Firebase FCM push notification in iOS iOS12 /迅速/收到的FCM PUSH消息不起作用 - iOS12 / swift / Received FCM PUSH Message not working iOS-从Firebase控制台未收到FCM推送通知 - IOS - FCM Push Notification not received from firebase console Firebase消息不在iOS上-FIRMessaging接收到数据消息,但FIRMessagingDelegate's-messaging:didReceiveMessage:未实现 - Firebase Message not on iOS - FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented Flutter FCM IOS 静默(仅数据)通知未收到 - Flutter FCM IOS silent(data only) notification not received
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM