firebase を使用して、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"
}
}
誰かが私を案内したり、いくつかの記事を共有したりできますか? どうもありがとうございました!