前言
本文只針對(duì)個(gè)推所做的總結(jié),其他推送大同小異
正文
1.添加Notification Service Extension
選擇File->New->Target->Notification Service Extension

image
選擇Activate即可,如果沒有彈出該選項(xiàng)框,需要自行添加相應(yīng)的 scheme。
需要注意的點(diǎn):
- Extension 的 Bundle Identifier 不能和 Main Target(也就是你自己的 App Target)的 Bundle Identifier 相同,否則會(huì)報(bào) BundeID 重復(fù)的錯(cuò)誤。
- Extension 的 Bundle Identifier 需要在 Main Target 的命名空間下,比如說 Main Target 的 BundleID 為 ent.getui.xxx,那么Extension的BundleID應(yīng)該類似與ent.getui.xxx.yyy這樣的格式。如果不這么做,會(huì)引起命名錯(cuò)誤。
可以使用.NotificationService的格式
NotificationService的 Deployment Target要設(shè)置最低10.0
2.添加請(qǐng)求代碼
之后項(xiàng)目中會(huì)多出一個(gè)文件夾NotificationService,其中包括一對(duì)關(guān)鍵文件
NotificationService.h和NotificationService.m
.m中會(huì)默認(rèn)有兩個(gè)方法
- (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);
}
- (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);
}
當(dāng)多媒體消息到達(dá)客戶端后會(huì)走第一個(gè)方法,可以在其中處理資源
但是如果處理時(shí)間過長(zhǎng),將會(huì)進(jìn)入serviceExtensionTimeWillExpire方法進(jìn)行最后的緊急處理。
NotificationService文件夾中的info.plist要添加App Transport Security Settings字典類型,并增加BOOL類型的Allow Arbitrary Loads設(shè)置為YES