Xcode|iOS10のUNUserNotificationCenterで画像添付で表示させてみよう

スポンサーリンク
Objective-C

20161103_003

こんにちは、あまのじゃくなHikaruapp(@hikaruapp)です。

iOS10から新しくなった通知は、画像を添付することができるようになりました。
画像だけではなく映像も可能です。

これは、ちょっと世界が広がりますね!

では、早速いじり倒していきましょう!

もちろん、Objective-Cです(笑)

通常の画像無しの通知はこちらをどうぞ

iOS10からのNotificationが変わったぞと聞いたので、どの様に変わったのか調べたら・・・。すべてs…

実行環境

Xcode 8.1
iOS 10.1
Mac OS X 10.11.6

画像添付する方法は、UNNotificationAttachmentを使用する

iOSらしいというか、なんかこう考えられているなという感じで使い方は簡単です。
と、その前に、画像か映像を用意しておいてくださいね。

さて、いきましょうか、

基本形のコード

// image
NSURL *urlImage =  [[NSBundle mainBundle] URLForResource:@"train" withExtension:@"mp4"];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"com.hikaruapp.testNotifer"
                                                                                      URL:urlImage
                                                                                  options:nil
                                                                                    error:nil];
content.attachments = @[attachment];

このコードは、ファイル名:train.mp4の映像をUNNotificationAttachmentクラスで作っていきます。最後に、content.attachmentsにArray形式で代入してあげます。

Array形式なので間違いなく

全体のコード

- (void)notiferRequestFiveSecondWithImag {
    
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title    = [NSString localizedUserNotificationStringForKey:@"Hello!"
                                                             arguments:nil];
    content.body     = [NSString localizedUserNotificationStringForKey:@"Yokohama Station"
                                                             arguments:nil];
    content.subtitle = [NSString localizedUserNotificationStringForKey:@"Hello_message_with_image"                                                             arguments:nil];
    content.sound    = [UNNotificationSound defaultSound];
    
    // image
    NSURL *urlImage =  [[NSBundle mainBundle] URLForResource:@"train" withExtension:@"mp4"];
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"com.hikaruapp.testNotifer"
                                                                                           URL:urlImage
                                                                                       options:nil
                                                                                         error:nil];
    content.attachments = @[attachment];
    
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:5
                                                  repeats:NO];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content
                                                                          trigger:trigger];
    
    // Schedule the notification.
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request
             withCompletionHandler:^(NSError * _Nullable error) {
                 if (!error) {
                     NSLog(@"notifer with image request sucess");
                 }
             }];
}

実行してみよう

5秒後に通知が来ました

20161103_001

右端に画像が表示されました。通知をした方向に引っ張ると

20161103_002

ムービーが表示されプレイを押すと再生されます。

これは、なかなか面白いものができそうです。

まとめ

どんどん面白くなっていくiOSな反面、覚えるのが大変になっていきますが、プログラマーとしても進化していきましょう!

GitHub

サンプルコード – github

 

コメント

タイトルとURLをコピーしました