Skip to content

通知服務擴展 ​

Object-C SDK 2.5.0403 以上版本及Swift SDK,需要集成通知服務擴展(Notification Service Extension)以支持完整的推送功能。


一、添加通知服務擴展 Target ​

  • 1、打開主工程 .xcodeproj 或 .xcworkspace
  • 2、點擊頂部菜單欄:File > New > Target...
  • 3、選擇:Notification Service Extension,點擊 Next
  • 4、命名,例如:NotificationServiceExtension,點擊 Finish 並激活
  • 5、創建後 Xcode 會自動生成擴展的 Info.plist、NotificationService.h、NotificationService.m 文件和 Target

二、擴展配置描述文件(Provisioning Profile) ​

主 App(com.example.myapp)與擴展(com.example.myapp.Notification)必須各自擁有獨立的描述文件(Provisioning Profile)。擴展的 Bundle Identifier 必須以主 App 的 Bundle Identifier 為前綴,並在其後加上自定義後綴

  • Project -> Targets -> NotificationServiceExtension -> Signing & Capabilities中點擊Provisioning Profile選項添加描述文件。效果如圖:

三、主App與擴展的版本管理 ​

  • 1、擴展的Minimum Deployments不能高於主App,建議部署版本與主App保持一致
  • 2、擴展與主App的Bundle version(CFBundleVersion)與 CFBundleShortVersionString 建議保持一致

五、擴展導入SDK和資源文件 ​

  • 1、Project -> Targets -> NotificationServiceExtension -> General,直接拖拽工程中的 Gamedreamer 到 Frameworks and Libraries並確保選中Do Not Embed,效果如圖:
  • 2、Project -> Targets -> NotificationServiceExtension -> Build Phases,直接拖拽工程中的 GamedreamerResources 到 Copy Bundle Resources,效果如圖:

六、引用SDK提供的通知服務擴展接口 ​

  • 在NotificationService.m文件的內容頂部的#import "NotificationService.h"下方寫上#import <Gamedreamer/Gamedreamer.h>代碼,並在- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler方法中調用[[GamedreamerManager shareInstance] gamedreamerdidReceiveNotificationRequest:request withContentHandler:self.contentHandler];

注意:新建的文件裡會有自動生成的預設範例代碼在 //Modify the notification content here... 註釋下,需要刪除並調用SDK提供的接口,也可以直接複製下方代碼,完整替換NotificationService.m文件中的內容

objective-c
#import "NotificationService.h"
#import <Gamedreamer/GamedreamerManager.h> //引用SDK
 
@interface NotificationService ()
 
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
 
@end
 
@implementation NotificationService
 
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    //調用通知服務擴展接口
    [[GamedreamerManager shareInstance] gamedreamerdidReceiveNotificationRequest:request withContentHandler:self.contentHandler];
}
 
- (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);
}
 
@end

七、打包導出IPA注意事項 ​

  • 1、使用主 App 的 scheme 打包,擴展會自動被打包到 IPA 內
  • 2、使用Xcode導出IPA時,需要同時添加主App和擴展的描述文件