我正在尝试使用 firebase 在我们的 React Native 应用程序中实现丰富的通知。react native 还不支持丰富的通知,所以我不得不在我的 NotificationServiceExtension 中使用 swift,在我的 AppDelegate.m 中使用 Objective C。我在这两个方面都没有经验。如果应用程序在后台或关闭,丰富的通知会正确显示,但如果应用程序在前台,即使我总是得到它们(使用断点),它们也不会显示。奇怪的是,有时他们会...
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let title = request.content.title
let subtitle = request.content.subtitle
let body = request.content.body
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = bestAttemptContent,
let attachmentURLAsString = bestAttemptContent.userInfo["image"] as? String,
let attachmentURL = URL(string: attachmentURLAsString) else {
return
}
downloadImageFrom(url: attachmentURL, title: title, subtitle: subtitle, body: body) { (attachment) in
if let attachment = attachment {
bestAttemptContent.attachments = [attachment]
contentHandler(bestAttemptContent)
}
}
}
private func downloadImageFrom(url: URL, title: String, subtitle: String, body: String, with completionHandler: @escaping (UNNotificationAttachment?) -> Void) {
let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
guard let downloadedUrl = downloadedUrl else {
completionHandler(nil)
return
}
var urlPath = URL(fileURLWithPath: NSTemporaryDirectory())
let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".png"
urlPath = urlPath.appendingPathComponent(uniqueURLEnding)
try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath)
let notificationContent = UNMutableNotificationContent()
notificationContent.title = title
notificationContent.subtitle = subtitle
notificationContent.body = body
notificationContent.sound = UNNotificationSound.default
notificationContent.badge = 0
do {
let attachment = try UNNotificationAttachment(identifier: "notifImg", url: urlPath, options: nil)
notificationContent.attachments = [attachment]
let request = UNNotificationRequest(identifier: "notif",
content: notificationContent,
trigger: nil)
UNUserNotificationCenter.current().add(request) { (error) in
completionHandler(attachment)
}
completionHandler(attachment)
} catch {
completionHandler(nil)
}
}
task.resume()
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
我从阅读的许多文章中获得了此代码。通知的批准请求在 AppDelegate 中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert)
categories:nil]];
[application registerForRemoteNotifications];
...
}
我正在使用https://fcm.googleapis.com/fcm/send发送通知,如下所示:
{
"notification": {
"mutable_content": true,
"title": "title",
"body": "body"
},
"to" : "__token__",
"data": {
"image": "https://ilyarm.ru/assets/949163b5edd92aa1ec0379734-697x403.jpg"
}
}
有人可以指导我或分享一些文章吗?非常感谢!