接收豐富推送通知

您可以將影像檔案附加至Apple通知。 新增視覺元件可大幅提升使用者與推播通知的互動。

若要在iOS應用程式中接收豐富式推播通知:

  1. 完成以下所述步驟,針對應用程式實施推送訊息: 推送訊息.

  2. 確認您可以傳送文字推播訊息至您的應用程式。

  3. 完成下列步驟以新增通知服務擴充功能:

    1. In your Xcode project, select File > New > Target.
    2. 選取​通知服務擴充功能
    3. 確認 NotificationService.m 檔案存在。
  4. 開啟 NotificationService.m 檔案,並確認下列委派方法存在:

    • 一種接收通知請求的方法。

    • 一種處理服務擴展到期的方法。

      若要接收豐富式推播通知,請使用第一種方法:

      (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
      

      在此方法中,您可以使用 attachment-url 索引鍵從 userInfo 取得媒體 URL。將檔案下載至本機目錄後,請將本機路徑新增至 bestAttemptContent.attachments

      以下是此方法中的程式碼範例:

      - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
      self.contentHandler = contentHandler;
      self.bestAttemptContent = [request.content mutableCopy];
         NSDictionary* userInfo = request.content.userInfo;
      if(userInfo[@"attachment-url"]){
         NSURL* url = [[NSURL alloc] initWithString:userInfo[@"attachment-url"]];
         NSURLSessionDownloadTask* task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
             if(!location){
                 return;
             }
             NSString* tmpDirectory = NSTemporaryDirectory();
             NSString* tmpFilePath = [NSString stringWithFormat:@"file://%@%d%d%@", tmpDirectory, arc4random_uniform(100000),
                                    arc4random_uniform(100000), [url lastPathComponent]];
             NSURL* tmpUrl = [[NSURL alloc] initWithString:tmpFilePath];
             NSError * fileError = nil;
             [[NSFileManager defaultManager] moveItemAtURL:location toURL:tmpUrl error:&fileError];
             if(fileError){
                 return;
             }
             UNNotificationAttachment* attachment = [UNNotificationAttachment attachmentWithIdentifier:@"video" URL:tmpUrl options:nil error:&fileError];
             if(fileError){
                 return;
             }
             self.bestAttemptContent.attachments = @[attachment];
             self.contentHandler(self.bestAttemptContent);
         }];
         [task resume];
       }
      }
      

For more information about rich push notifications with iOS, see UNNotificationAttachment.

本頁內容

Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now
Adobe Maker Awards Banner

Time to shine!

Apply now for the 2021 Adobe Experience Maker Awards.

Apply now