如何在iOS 10应用程序的推送通知中添加媒体附件? [英] How can I add media attachments to my push notifications in an iOS 10 app?

查看:145
本文介绍了如何在iOS 10应用程序的推送通知中添加媒体附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多个示例,说明如何设置项目以添加使用媒体附件"技术显示图像的丰富通知.我已经阅读了大多数内容,但是却错过了一些事情,因为我的项目没有显示带有此有效负载的任何丰富的通知(已通过APNS-Tool和Boodle测试):

{
    "aps":{
        "alert":{
            "title": "Rich test",
            "body": "Dancing Banana"
        },
        "mutable-content": 1
    }
}

该通知可见,但是在3D Touch上,没有显示其他信息(如图像或修改后的标题).通知扩展断点和NSLog消息也不起作用.

这是我的演示项目: https://github.com/gklka/RichTest

我所做的:

  1. 创建了一个新项目
  2. 实施的iOS风格的授权:

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted) {
            NSLog(@"Granted");

            [[UIApplication sharedApplication] registerForRemoteNotifications];

        } else {
            NSLog(@"Error registering: %@", error);
        }

    }];

  1. 将调试添加到AppDelegate:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Token: %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error);
}

  1. 为项目添加了新目标:Notification Service扩展:

  1. 已将banana.gif添加到项目中

  2. 添加了将香蕉附件添加到NotificationService.m

  3. 的代码

    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

    // Add image attachment
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"banana" withExtension:@"gif"];
    NSError *error;
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"banana" URL:fileURL options:nil error:&error];
    self.bestAttemptContent.attachments = @[attachment];

我想念什么?

其他信息:当我使用本地通知而不是远程通知时,同样的事情也起作用.

解决方案

您已准备就绪.您发送附件的方式通常是在有效负载中作为URL.但是,如果您想像附件那样对附件进行硬编码,我认为它可以工作,但我认为您错过了一个关键组件.我认为服务扩展无法访问主捆绑包或其资源.如果您将资源添加到服务扩展中并尝试加载该资源(使用诸如[NSBundle bundleForClass:[NotificationService class]]之类的东西),我怀疑它会起作用.

但是,如果您将URL作为有效负载的一部分发送,那么您将从该URL而不是捆绑包中加载图像.在那种情况下,我很确定您还必须在NSURL上使用startAccessingSecurityScopedResource(以及stopAccessingSecurityScopedResource).

希望有帮助!

There are multiple examples how you should set up your project to add rich notifications which use 'media attachments' technology to show images. I've read most of them but something I missed, because my project does not display any rich notifications with this payload (tested with APNS-Tool and Boodle):

{
    "aps":{
        "alert":{
            "title": "Rich test",
            "body": "Dancing Banana"
        },
        "mutable-content": 1
    }
}

The notification is visible, but on 3D Touch, there are no additional infos displayed (like the image or the modified title). The notification extension breakpoints and NSLog messages are also not working.

Here is my demo project: https://github.com/gklka/RichTest

What I've done:

  1. Created a new project
  2. implemented iOS-style authorization:

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted) {
            NSLog(@"Granted");

            [[UIApplication sharedApplication] registerForRemoteNotifications];

        } else {
            NSLog(@"Error registering: %@", error);
        }

    }];

  1. added debug to AppDelegate:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Token: %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error);
}

  1. Added a new target to the project: Notification Service Extension:

  1. Added banana.gif to the project

  2. Added code to add banana attachment into NotificationService.m

    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

    // Add image attachment
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"banana" withExtension:@"gif"];
    NSError *error;
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"banana" URL:fileURL options:nil error:&error];
    self.bestAttemptContent.attachments = @[attachment];

What did I miss?

Additional info: The same thing works when I use local notifications instead of remote notifications.

解决方案

You're most of the way there. The way you're going to send the attachment is usually as a URL in your payload. However, if you wanted to hard-code the attachment, like your code does, I think it would work, but I think you missed one critical component. I think the service extension does not have access to the main bundle or its resources. If you added a resource to the service extension and tried to load that (using something like [NSBundle bundleForClass:[NotificationService class]]), I suspect it would work.

However, if you send the URL as part of the payload, then you're going to load the image from that URL and not the bundle anyway. In that case, I'm pretty sure you also have to use startAccessingSecurityScopedResource on NSURL (along with stopAccessingSecurityScopedResource).

Hope that helps!

这篇关于如何在iOS 10应用程序的推送通知中添加媒体附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆