前言
本文只針對個推所做的總結(jié),其他推送大同小異
正文
1.添加Notification Service Extension
選擇File
->New
->Target
->Notification Service Extension
image
選擇Activate即可粟害,如果沒有彈出該選項框轧房,需要自行添加相應的 scheme。
需要注意的點:
- Extension 的 Bundle Identifier 不能和 Main Target(也就是你自己的 App Target)的 Bundle Identifier 相同亿虽,否則會報 BundeID 重復的錯誤。
- Extension 的 Bundle Identifier 需要在 Main Target 的命名空間下,比如說 Main Target 的 BundleID 為 ent.getui.xxx似忧,那么Extension的BundleID應該類似與ent.getui.xxx.yyy這樣的格式。如果不這么做丈秩,會引起命名錯誤盯捌。
可以使用.NotificationService的格式
NotificationService的 Deployment Target要設(shè)置最低10.0
2.添加請求代碼
之后項目中會多出一個文件夾NotificationService
,其中包括一對關(guān)鍵文件
NotificationService.h
和NotificationService.m
.m中會默認有兩個方法
- (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);
}
當多媒體消息到達客戶端后會走第一個方法蘑秽,可以在其中處理資源
但是如果處理時間過長饺著,將會進入serviceExtensionTimeWillExpire
方法進行最后的緊急處理箫攀。
NotificationService文件夾中的info.plist要添加App Transport Security Settings字典類型,并增加BOOL類型的Allow Arbitrary Loads設(shè)置為YES