您可以將影像檔案附加至Apple通知。 新增視覺元件可大幅提升使用者與推播通知的互動。
若要在iOS應用程式中接收豐富推送通知:
完成以下所述步驟,針對應用程式實施推送訊息: 推送訊息.
確認您可以傳送文字推送訊息至您的應用程式。
完成下列步驟,新增通知服務擴充功能:
NotificationService.m
檔案存在。開啟 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];
}
}
如需iOS豐富推送通知的詳細資訊,請參閱UNNotificationAttachment。