当我的应用程序在后台或被终止时,我需要显示一个通知。我使用firebase-cpp-sdk在iOS上显示FCM‘通知’消息没有问题,我尝试了quickstart-cpp/messaging/testapp,它就这样工作了。但是,当应用程序在后台或前台时收到'data‘消息时-没有显示通知,我只在日志中看到收到消息。
正如许多答案所建议的那样,我在消息中使用"content_available": true。这有助于实际接收'data‘消息,正如我在日志中看到的那样,但该消息并未显示。我尝试了遗留的HTTP和HTTP v1协议,结果是一样的。传统HTTP消息的一个示例是:
{
"data": {
"body": "Entrance door Intrusion at 2 Jun 2020 01:32:08",
"title": "Intrusion"
},
"content_available": true,
"to": "fcm_device_token_here"
}我需要像Android一样手动创建通知吗?或者有其他方法可以做到这一点呢?
发布于 2020-06-10 14:54:45
为了回答我自己的问题--是的,我创建了一个本地通知。为此,我使用了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");
}
}];https://stackoverflow.com/questions/62182156
复制相似问题