web-dev-qa-db-ja.com

iOSプッシュ通知で画像を表示する方法は?

iOS 10は、プッシュ通知フレームワークの更新を導入しました。

UserNotificationsUI.framework

Apple docsに書かれているように、ユーザーのデバイスに表示されるローカルおよびリモート通知の外観をカスタマイズできます。

だから誰かがロック画面でプッシュ通知に画像を表示する方法を考えているなら。 andoridプッシュ通知が行っているのと同じです。

おかげで、

44
Parth Pandya

ローカルおよびリモート通知の外観をカスタマイズする場合は、次の手順を実行します。

  1. UNNotificationCategoryを作成し、UNUserNotificationCenterカテゴリーに追加します。

    let newCategory = UNNotificationCategory(identifier: "newCategory",
                                             actions: [ action ],
                                             minimalActions: [ action ],
                                             intentIdentifiers: [],
                                             options: [])
    
    let center = UNUserNotificationCenter.current()
    
    center.setNotificationCategories([newCategory])
    
  2. UNNotificationContentExtensionを作成します。

enter image description here

次に、コードまたはストーリーボードを使用してUIViewControllerをカスタマイズします。

  1. カテゴリをUNNotificationContentExtensionのplistに追加します。

enter image description here

4.プッシュ通知

ローカル通知

UNMutableNotificationContentを作成し、categoryIdentifierのカテゴリとUNUserNotificationCenterのplistを含むUNNotificationContentExtensionを「newCategory」に設定します。

let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"

let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)

let center = UNUserNotificationCenter.current()
center.add(request)

リモート通知

"mutable-content : 1"および"category : newCategory"を設定します。カテゴリ値は、以前にUNUserNotificationCenterおよびUNNotificationContentExtensions plistに追加したものと一致する「newCategory」に設定されていることに注意してください。

例:

 {
    "aps" : {
        "alert" : {
        "title" : "title",
        "body" : "Your message Here"
        },
        "mutable-content" : "1",
        "category" : "newCategory"
    },
    "otherCustomURL" : "http://www.xxx.jpg"
 }
  1. 注:3DTouchをサポートするデバイスまたはシミュレーターが必要です。そうしないと、カスタムUNNotificationContentExtension viewcontrollerを表示できません。(iOS10 Beta1では機能しません。しかし、今では3Dタッチなしでこの作業を行います)

そして...ロック画面に表示されたプッシュ通知に画像を表示したいだけなら、UNNotificationAttachmentを追加する必要があります:

let content = UNMutableNotificationContent()
content.title = ...
content.body = ...
content.categoryIdentifier = "newCategory"

let fileURL: URL = ... //  your disk file url, support image, audio, movie

let attachement = try? UNNotificationAttachment(identifier: "attachment", url: fileURL, options: nil)
content.attachments = [attachement!]

let request = UNNotificationRequest.init(identifier: "newNotificationRequest", content: content, trigger: nil)

let center = UNUserNotificationCenter.current()
center.add(request)

enter image description here

詳細機能については、 デモ

80
maquannene

実際、ローカル通知を設定していて、通知自体に画像を表示したいだけの場合は、NotificationsUI.frameworkまたはUNNotificationContentExtensionsを気にする必要はまったくありません。これは、ユーザーが通知を3Dでタッチまたは展開するときにカスタムUIが必要な場合にのみ役立ちます。

デバイスに既にある画像を追加するには(アプリに同梱されているか、通知が作成される前のある時点でアプリによって生成/保存されます)、パスが終了するファイルURLを持つUNNotificationAttachmentを追加するだけです.png(または.jpgでも動作しますか?)このようなもの:

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Title";
content.body = @"Body";
content.sound = [UNNotificationSound defaultSound];
NSURL *imageURL = [NSURL URLWithString:@"file:/some/path/in/app/image.png"];
NSError *error;
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageURL options:nil error:&error];
if (error)
{
    NSLog(@"error while storing image attachment in notification: %@", error);
}
if (icon)
{
    content.attachments = @[icon];
}

次に、通知が表示されると、メッセージ通知の場合と同様に、通知バナーの右側に画像が表示されます。また、categoryIdentifierなどを使用してコンテンツ拡張機能を設定するすべてのフープをジャンプする必要はありません。

編集:これはローカル通知の有効なソリューションにすぎないことを追加するために更新されました。

10
Greg G

プッシュ通知を作成するとき、および処理するときは、いくつかの作業を行う必要があります。

  1. ペイロードを作成する場合、以下のような追加の属性添付ファイルを追加する必要があります。

    {        
        aps : {
            alert: { },
            mutable-content:1
        }
        my-attachment = "url to resource"
    }
    
  2. 通知システムを受け取ったら、サービス拡張のdidReceiveメソッドを呼び出し、通知拡張didReceiveメソッドを次のようにオーバーライドします

    public func didReceive(_ request:UNNotificationRequest, withContentHandler contentHandler:(UNNotificatinContent) -> Void) {
        let fileUrl = //
        let attachment = UNNotificationAttachment(identifier : "image", url: fileUrl, options: nil)
        let content = request.content.mutableCopy as! UNMutableNotificationContent
        content.attachment = [attachment]
        contentHandler(content)
    }
    

ここ は、このトピックに関するWWDCビデオトークです。

4
Adnan Aftab