如何在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.

需要哪个密钥才能使用设置推送通知的有效负载?

Which key is required to set payload of push notification ?

如何识别有效负载以及如何从推送通知启动服务扩展?

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

让我一步一步来。

UNNotificationServiceExtension - 它是什么?

UNNotificationServiceExtension是一个App Extenstion目标,它与您的应用程序捆绑在一起,目的是在将推送通知交付给设备之前修改推送通知,然后再将其呈现给用户。您可以通过下载或使用应用程序中捆绑的附件来更改标题,副标题,正文以及推送通知的附件。

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

设置推送通知的有效负载需要哪个密钥?

您需要设置可变内容标记为 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)

如何识别有效负载以及如何从推送通知启动服务扩展?

构建扩展程序,然后构建并运行您的应用程序。发送推送通知, 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.

远程通知的原始有效负载是通过函数参数 request $ c $的 request.content 属性传递的。 c>。

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": ""
}

请注意附件 - url key是您自己关注的自定义密钥,iOS无法识别。

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