Notification Service Extensionを利用して動画つきプッシュを受信しようとしていますが、動画のついていないプッシュが受信されてしまっています。
静止画像(.jpg)やGIF画像(.gif)は想定通り画像付きプッシュの受信が行えるのですが、Payloadの内容のメディアのURLに動画(.mpg,.mpeg,.m2v,.avi)を指定すると動画が添付されず文字列のみのプッシュが受信されます。
このとき、メディアのURL以外は何も変更をしていません。
動画のサイズについては全て6MB以下、3秒ほどの動画でテストを行っています。
メディアは静止画・GIF画像・動画全てS3の同じバケットに存在するものをhttps通信で取得できるようにしています。
<環境・条件>
macOS Sierra(ver10.12.6)
Xcode9.1
DeploymentTarget: iOS10.3 ※テストアプリとExtensionは常に同じDeploymentTargetを指定
テスト端末:iPhone6(iOS11.0.3)、iPhone8(iOS11.1.2) ※両方同じ挙動
Payloadの内容
json
1{ 2 "aps":{ 3 "alert": { 4 "title": "This is title.", 5 "subtitle": "This is subtitle.", 6 "body": "This is body." 7 }, 8 "sound": "default", 9 "mutable-content": 1 10 }, 11 "media": "(メディアのURL)" 12}
Extensionの内容
swift
1class NotificationService: UNNotificationServiceExtension { 2 let imageKey = "media" 3 4 var contentHandler: ((UNNotificationContent) -> Void)? 5 var bestAttemptContent: UNMutableNotificationContent? 6 7 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 8 self.contentHandler = contentHandler 9 bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) 10 11 if let imageUrlString = request.content.userInfo[imageKey] as? String { 12 13 let imageUrl = URL(string: imageUrlString)! 14 let session = URLSession(configuration: URLSessionConfiguration.default) 15 let task = session.dataTask(with: imageUrl, completionHandler: {[weak self](data, response, error) in 16 do { 17 if let writePath = NSURL(fileURLWithPath:NSTemporaryDirectory()) 18 .appendingPathComponent(imageUrl.lastPathComponent) { 19 try data?.write(to: writePath) 20 21 let identifier = "push_identifier" 22 if let bestAttemptContent = self?.bestAttemptContent { 23 let attachment = try UNNotificationAttachment(identifier: identifier, url: writePath, options: nil) 24 bestAttemptContent.attachments = [attachment] 25 contentHandler(bestAttemptContent) 26 } 27 } else { 28 if let bestAttemptContent = self?.bestAttemptContent { 29 contentHandler(bestAttemptContent) 30 } 31 } 32 } catch let error as NSError { 33 print(error.localizedDescription) 34 35 if let bestAttemptContent = self?.bestAttemptContent { 36 contentHandler(bestAttemptContent) 37 } 38 } 39 }) 40 task.resume() 41 } else { 42 if let bestAttemptContent = self.bestAttemptContent { 43 contentHandler(bestAttemptContent) 44 } 45 } 46 } 47 48 override func serviceExtensionTimeWillExpire() { 49 // Called just before the extension will be terminated by the system. 50 // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. 51 if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { 52 contentHandler(bestAttemptContent) 53 } 54 } 55}
以上よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。