首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS上显示使用firebase-cpp-sdk接收到的FCM 'data‘消息?

如何在iOS上显示使用firebase-cpp-sdk接收到的FCM 'data‘消息?
EN

Stack Overflow用户
提问于 2020-06-04 04:26:08
回答 1查看 83关注 0票数 0

当我的应用程序在后台或被终止时,我需要显示一个通知。我使用firebase-cpp-sdk在iOS上显示FCM‘通知’消息没有问题,我尝试了quickstart-cpp/messaging/testapp,它就这样工作了。但是,当应用程序在后台或前台时收到'data‘消息时-没有显示通知,我只在日志中看到收到消息。

正如许多答案所建议的那样,我在消息中使用"content_available": true。这有助于实际接收'data‘消息,正如我在日志中看到的那样,但该消息并未显示。我尝试了遗留的HTTP和HTTP v1协议,结果是一样的。传统HTTP消息的一个示例是:

代码语言:javascript
复制
{
    "data": {
        "body": "Entrance door Intrusion at 2 Jun 2020 01:32:08",
        "title": "Intrusion"
    },
    "content_available": true,
    "to": "fcm_device_token_here"
}

我需要像Android一样手动创建通知吗?或者有其他方法可以做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-10 14:54:45

为了回答我自己的问题--是的,我创建了一个本地通知。为此,我使用了qt-notification库,因为我使用的是QT,但显示通知的代码示例如下(取自项目):

代码语言:javascript
复制
// 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");
    }
}];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62182156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档