画像ファイルをAppleの通知に添付できます。 ビジュアルコンポーネントを追加すると、プッシュ通知を使用したユーザーの関与が大幅に増加する可能性があります。
iOSアプリでリッチプッシュ通知を受信するには:
アプリにプッシュメッセージを実装するため、プッシュメッセージの手順を完了します。
テキストのプッシュメッセージをアプリに送信できることを確認します。
Notification Service追加の拡張機能を使用するには、次の手順を実行します。
NotificationService.m
ファイルが存在することを確認します。NotificationService.m
ファイルを開き、以下の Delegate メソッドが存在することを確認します。
通知要求を受け取る1つのメソッド。
サービス拡張の有効期限を処理する1つのメソッド。
リッチプッシュ通知を受信するには、最初の方法を使用します。
(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.