0

私はUNNotificationServiceExtensionをアプリケーションに統合しましたが、画像などの追加コンテンツはありません。また、画像の読み込みを正しく実装していないと考え、テストの名前と本文のペイロードを変更しようとしました。しかし、それは私に結果を与えてくれませんでした。私は、FirebaseコンソールとFirebaseクラウドファンクションを通じてプッシュ通知を実装しています。私もこのトピックに関するいくつかの記事を読むが、彼らは私を助けなかった。どのように修正することができますか教えてください?UNNotificationServiceExtensionはFirebase Cloud機能と連携しません

class NotificationService: UNNotificationServiceExtension { 

    var contentHandler: ((UNNotificationContent) -> Void)? 
    var bestAttemptContent: UNMutableNotificationContent? 


    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 
     self.contentHandler = contentHandler 
     bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) 

     if let bestAttemptContent = bestAttemptContent { 
      // Modify the notification content here... 
      bestAttemptContent.title = "Apple [modified]" 
      bestAttemptContent.body = "Xcode" 
      let attachmentStorage = AttachmentStorage() 

      if let jpeg = request.content.userInfo["image"] as? String { 
       guard let url = URL(string: jpeg) else { 
        contentHandler(bestAttemptContent) 
        return 
       } 
       debugPrint("url", url) 
       attachmentStorage.store(url: url, extension: "jpeg") { (path, error) in 
        if let path = path { 
         do { 
          let attachment = try UNNotificationAttachment(identifier: "image", url: path, options: nil) 
          bestAttemptContent.attachments = [attachment] 
          contentHandler(bestAttemptContent) 
          return 
         } catch { 
          contentHandler(bestAttemptContent) 
          return 
         } 
        } 
       } 
      } 

      if let png = request.content.userInfo["png"] as? String { 
       guard let url = URL(string: png) else { 
        contentHandler(bestAttemptContent) 
        return 
       } 
       attachmentStorage.store(url: url, extension: "png") { (path, error) in 
        if let path = path { 
         do { 
          let attachment = try UNNotificationAttachment(identifier: "image", url: path, options: nil) 
          bestAttemptContent.attachments = [attachment] 
          contentHandler(bestAttemptContent) 
          return 
         } catch { 
          contentHandler(bestAttemptContent) 
          return 
         } 
        } 
       } 
      } 

      contentHandler(bestAttemptContent) 
     } 
    } 

    override func serviceExtensionTimeWillExpire() { 
     // Called just before the extension will be terminated by the system. 
     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. 
     if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { 
      contentHandler(bestAttemptContent) 
     } 
    } 

} 

class AttachmentStorage { 

    func store(url: URL, extension: String, completion: ((URL?, Error?) ->())?) { 
     // obtain path to temporary file 
     let filename = ProcessInfo.processInfo.globallyUniqueString 

     let path = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(filename).\(`extension`)") 

     // fetch attachment 
     let task = URLSession.shared.dataTask(with: url) { (data, response, error) in 
      let _ = try! data?.write(to: path) 
      completion?(path, error) 
     } 
     task.resume() 
    } 

} 

クラウド機能

const payload = { 
      notification: { 
       title: 'It’s decision time!', 
       body: 'text' 
       sound: 'default', 
       mutable_content: true 
      }, 
      data: { 
       image: "https://example.com/static_logos/320x320.png" 
      } 
     }; 

Push notification from firebase console

からペイロード更新日:私はまた、私はそれを修正し、このissue

答えて

0

をお読みください。 Firebaseのエンジニアがその文字列に当てはまるとは思えませんでした。

const payload = { 
        notification: { 
         title: 'It’s decision time!', 
         body: "text" 
         sound: 'default', 
         mutable_content: 'true' 
        }, 
        data: { 
         image: "example" 
        } 
       }; 
関連する問題