如何在 iOS10 中使用带有 UNNotification 的通知服务扩展 [英] How to use Notification service extension with UNNotification in iOS10

查看:23
本文介绍了如何在 iOS10 中使用带有 UNNotification 的通知服务扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple 引入了新的扩展名称 "UNNotificationServiceExtension",但是如何从推送中启动它通知?

Apple introduce new extension names "UNNotificationServiceExtension", but how to launch it from push notification ?

我读到服务扩展为有效负载提供端到端加密.

I read that service extension provide end to end encryption for payload.

设置推送通知的payload需要哪个key?

Which key is required to set payload of push notification ?

如何识别payload以及如何从推送通知中启动服务扩展?

How to identify payload and how to launch service extension from push notification ?

推荐答案

让我一步一步来.

UNNotificationServiceExtension - 它是什么?

UNNotificationServiceExtension 是一个应用程序扩展目标,您与您的应用程序捆绑在一起,旨在在推送通知传送到设备之前修改推送通知,然后再将其呈现给用户.您可以更改标题、副标题、正文,还可以通过下载或使用应用中捆绑的附件向推送通知添加附件.

UNNotificationServiceExtension is an App Extenstion target that you bundle along with your app aiming to modify the push notifications as and when they are delivered to the device before rendering it to the user. You can change the title, subtitle, body and additionally add attachments to the push notification by either downloading it or using one bundled in the app.

如何创建

转到文件 -> 新建 -> 目标 -> 通知服务扩展并填写详细信息

Go to File -> New -> Target -> Notification Service Extension and fill in the details

设置推送通知的有效载荷需要哪个键?

您需要将 mutable-content 标志设置为 1 以触发服务扩展.此外,如果content-available 设置为1,则服务扩展将不起作用.所以要么不设置,要么设置为 0.(这不适用.您可以设置或取消设置 content-available 标志)

You need to set the mutable-content flag to 1 to trigger the service extension. Also, if the content-available is set to 1, the service extension will not work. So either don't set it or set it to 0. ( This is not applicable. You can set or unset content-available flag)

如何识别payload以及如何通过推送通知启动服务扩展?

构建扩展,然后构建并运行您的应用.发送将 mutable-content 设置为 1 的推送通知.

Build the extension and then build and run your app. Send a push notification with the mutable-content set to 1.

代码

UNNotificationService 公开了两个函数:

UNNotificationService exposes two functions:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
               withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler;

- (void)serviceExtensionTimeWillExpire;

第一个函数在设备上接收到推送通知时触发,在它呈现给用户之前.你在函数内部的代码有机会在这个函数内部修改推送通知的内容.

The first function is triggered when the push notification is received on the device and before it is presented to the user. You code inside the function has the opportunity to modify the content of the push notification inside this function.

您可以通过修改扩展程序的 bestAttemptContent 属性来完成此操作,该属性是 UNNotificationContent 的一个实例,并且具有以下属性:title副标题正文附件

You do this by modifying the bestAttemptContent property of your extension which is an instance of UNNotificationContent and has properties: title, subtitle, body, attachments etc.

远程通知的原始payload通过函数参数requestrequest.content属性传递.

The original payload of the remote notification is delivered via request.content property of function parameter request.

最后,您使用 contentHandler 发送您的 bestAttemptContent:

Finally you dispatch your bestAttemptContent using the contentHandler:

self.contentHandler(self.bestAttemptContent); 

在第一种方法中,您的时间有限.如果时间到了,您的第二个方法将被调用,并使用您的代码迄今为止所做的最佳尝试.

You have limited time to do your stuff in the first method. In case that time expires your second method is called with the best attempt your code had made thus far.

示例代码

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
               withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    self.contentHandler(self.bestAttemptContent);
}

以上代码将[修改]附加到 PN 负载中的原始标题.

The above code appends [modified] to the original title in the PN payload.

示例有效载荷

{
    "aps": {
        "alert": {
            "title": "Hello",
            "body": "body.."
        },
        "mutable-content":1,
        "sound": "default",
        "badge": 1,

    },
  "attachment-url": ""
}

请注意,attachment-url 键是您自己关注的自定义键,iOS 无法识别.

Please note that the attachment-url key is a custom key for your own concerns and not recognised by iOS .

这篇关于如何在 iOS10 中使用带有 UNNotification 的通知服务扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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