閱讀1066 返回首頁    go 阿裏雲 go 技術社區[雲棲]


iOS SDK配置__iOS SDK手冊_App SDK 手冊_移動推送-阿裏雲

1. 創建應用

到阿裏雲移動推送控製台創建應用,應用創建完成後,進入移動推送相關模塊進行設置,具體操作請參見 創建APP

2. SDK下載和集成

2.1 SDK下載

  • 在移動推送控製台進行SDK下載;

ios-sdk-download

2.2 SDK引用說明

2.2.1 公共包依賴

  • libz.tbd
  • libresolv.tbd
  • CoreTelephony.framework
  • SystemConfiguration.framework
  • libsqlite3.tbd(阿裏雲平台下載的SDK無需依賴,百川平台下載的SDK需要依賴)

2.2.2 SDK目錄結構

  • CloudPushSDK.framework
  • AlicloudUtils.framework
  • UTDID.framework
  • UTMini.framework(阿裏雲平台下載的SDK無需依賴,百川平台下載的SDK需要依賴)

2.2.3 引入Framework

  • Xcode中,直接把下載SDK目錄中的framework拖入對應Target下即可,在彈出框勾選Copy items if needed
  • Build Phases -> Link Binary With Libraries中,引入2.2.1列出的公共包;

2.2.4 工程引入頭文件

  1. #import <CloudPushSDK/CloudPushSDK.h>

2.2.5 說明

  • 應用的targets -> Build Settings -> Linking -> Other Linker Flags,請加上-ObjC這個屬性,否則推送服務無法正常使用
  • iOS 9引入了App Transport Security(ATS)機製,可參考App Transport Security(ATS)機製。 要求App內訪問的網絡必須使用HTTPS協議,現在阿裏移動推送使用的是HTTP安全的加簽訪問機製來保證安全,未使用HTTPS,所以解決方法如下:
    • 在工程的Info.plist中添加NSAppTransportSecurity,添加後會自動轉變為App Transport Security Setting,右擊該選項選擇Show Raw Key/Values,可顯示原本添加名;
    • 將該選項點擊鋪開(黑色三角指向下),右擊選擇Add Row,會自動顯示Item Allow Arbitrary Loads,將Value值設為YES
  • 移動推送iOS SDK已經完成ATS適配,請求都以HTTPS發出,無需在Info.plist中進行ATS配置。

3. Push SDK使用

  • 請參照以下代碼完成SDK的初始化,其中appKey/appSecret的獲取參考創建App
  1. - (void)initCloudPush {
  2. // SDK初始化
  3. [CloudPushSDK asyncInit:@"*****" appSecret:@"*****" callback:^(CloudPushCallbackResult *res) {
  4. if (res.success) {
  5. NSLog(@"Push SDK init success, deviceId: %@.", [CloudPushSDK getDeviceId]);
  6. } else {
  7. NSLog(@"Push SDK init failed, error: %@", res.error);
  8. }
  9. }];
  10. }
  • 向蘋果APNs注冊獲取deviceToken並上報到阿裏雲推送服務器;
  1. /**
  2. * 注冊蘋果推送,獲取deviceToken用於推送
  3. *
  4. * @param application
  5. */
  6. - (void)registerAPNS:(UIApplication *)application {
  7. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
  8. // iOS 8 Notifications
  9. [application registerUserNotificationSettings:
  10. [UIUserNotificationSettings settingsForTypes:
  11. (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
  12. categories:nil]];
  13. [application registerForRemoteNotifications];
  14. }
  15. else {
  16. // iOS < 8 Notifications
  17. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
  18. (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
  19. }
  20. }
  21. /*
  22. * 蘋果推送注冊成功回調,將蘋果返回的deviceToken上傳到CloudPush服務器
  23. */
  24. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  25. [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
  26. if (res.success) {
  27. NSLog(@"Register deviceToken success.");
  28. } else {
  29. NSLog(@"Register deviceToken failed, error: %@", res.error);
  30. }
  31. }];
  32. }
  33. /*
  34. * 蘋果推送注冊失敗回調
  35. */
  36. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  37. NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
  38. }
  • 推送消息到來監聽;
  1. /**
  2. * 注冊推送消息到來監聽
  3. */
  4. - (void)registerMessageReceive {
  5. [[NSNotificationCenter defaultCenter] addObserver:self
  6. selector:@selector(onMessageReceived:)
  7. name:@"CCPDidReceiveMessageNotification"
  8. object:nil];
  9. }
  10. /**
  11. * 處理到來推送消息
  12. *
  13. * @param notification
  14. */
  15. - (void)onMessageReceived:(NSNotification *)notification {
  16. CCPSysMessage *message = [notification object];
  17. NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
  18. NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
  19. NSLog(@"Receive message title: %@, content: %@.", title, body);
  20. }

通知打開監聽

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // 點擊通知將App從關閉狀態啟動時,將通知打開回執上報
  3. // [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1)
  4. [CloudPushSDK sendNotificationAck:launchOptions];
  5. return YES;
  6. }
  7. /*
  8. * App處於啟動狀態時,通知打開回調
  9. */
  10. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
  11. NSLog(@"Receive one notification.");
  12. // 取得APNS通知內容
  13. NSDictionary *aps = [userInfo valueForKey:@"aps"];
  14. // 內容
  15. NSString *content = [aps valueForKey:@"alert"];
  16. // badge數量
  17. NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
  18. // 播放聲音
  19. NSString *sound = [aps valueForKey:@"sound"];
  20. // 取得Extras字段內容
  21. NSString *Extras = [userInfo valueForKey:@"Extras"]; //服務端中Extras字段,key是自己定義的
  22. NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras);
  23. // iOS badge 清0
  24. application.applicationIconBadgeNumber = 0;
  25. // 通知打開回執上報
  26. // [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1)
  27. [CloudPushSDK sendNotificationAck:userInfo];
  28. }

如果使用推送模塊,請參考移動推送常見問題

最後更新:2016-12-15 18:25:21

  上一篇:go iOS推送證書設置__iOS SDK手冊_App SDK 手冊_移動推送-阿裏雲
  下一篇:go iOS API介紹__iOS SDK手冊_App SDK 手冊_移動推送-阿裏雲