iOS NotificationServiceExtension

2023-01-16  本文已影响0人  唐人街的乞丐

后台语音播报会用到此功能或者其他图片或音视自定义推送会使用到NotificationServiceExtension

后台推送的时候需要添加mutable-content=1字段

创建NotificationServiceExtension
file--new--target


image.png

选择Notification Service Extension


image.png

下一步


image.png
下一步
image.png

此时多了一个


image.png

需要做的自定义处理

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

    
    self.contentHandler(self.bestAttemptContent);
}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}

如果需要做http网络请求,需要TestNotificationServiceExtension 里面的Info添加App Transport Security Settings

TestNotificationServiceExtension的Info和项目里的数据不互通
如果需要跟项目里的数据互通需要创建App Groups

参考 App Groups 创建 https://www.jianshu.com/p/3fbaa8143a75?v=1675044627433

上一篇 下一篇

猜你喜欢

热点阅读