10分鐘集成iOS極光推送SDK

配置:

  1. 注冊極光賬號并于控制臺創建應用,獲取對應應用的AppKey
  2. 在極光控制臺中的推送設置里上傳對應推送證書
  3. 使用pod 'JPush', '3.0.2' 導入極光推送SDK
  4. Xcode設置,target→Capabilities→Push NOtifications→ON
  5. Xcode設置,target→Capabilities→Background Modes→ON→Remote notifications打鉤

代碼:

請將以下代碼添加到AppDelegate.m引用頭文件的位置。

// 引入JPush功能所需頭文件
#import "JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import <AdSupport/AdSupport.h>

為AppDelegate中添加委派。

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

在didFinishLaunchingWithOptions中調用[self initJpushSDK:launchOptions];

// 極光推送
- (void)initJpushSDK:(NSDictionary *)launchOptions
{
    JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定義categories
        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
    [JPUSHService setupWithOption:launchOptions
                           appKey:@"1aed1329cf7f8c29730ca75c"
                          channel:@"App Store"
                 apsForProduction:YES
            advertisingIdentifier:nil];
    
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        
        if (resCode == 0) {
            // [USERDEFAULT setValue:registrationID forKey:kJPushRegistrationID];
            // [USERDEFAULT synchronize];
        }else {
            NSLog(@"registrationID獲取失敗,code:%d",resCode);
        }
    }];
    
    // 獲取自定義消息
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
}

#pragma mark 【 獲取自定義消息內容 】
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    
    NSDictionary * userInfo = [notification userInfo];
    
    NSLog(@"自定義message:%@",userInfo);
}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // 注冊 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

#pragma mark【 JPUSHRegisterDelegate 】
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置
    NSLog(@"userInfo111:%@\n%@\n%@\n%@",userInfo,userInfo[@"title"],userInfo[@"content"],userInfo[@"extras"]);
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            // 程序運行時收到通知,先彈出消息框
            NSLog(@"程序在前臺");
        }else{
            // 跳轉到指定頁面
            // 這里也可以發送個通知,跳轉到指定頁面
            [self goToMssageViewControllerWith:userInfo];
        }
    }
    completionHandler();  // 系統要求執行這個方法
}

- (void)goToMssageViewControllerWith:(NSDictionary*)userInfo {
   // 此處用于處理點擊消息跳轉界面的效果
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 不同版本極光推送SDK集成各有差異,集成時一定要注意版本號,樓主已將博文更新成最新的SDK JPush v3.0....
    i順頌時宜閱讀 7,902評論 37 170
  • 推送技術產生場景: --服務器端主動性: 客戶端與服務器交互都是客戶端主動的, 服務器一般不能主動與客戶端進行數據...
    原軍鋒閱讀 34,804評論 4 60
  • ** Tips:** 不同版本極光推送SDK集成各有差異,各位童鞋在集成時一定要注意版本號,本人集成的是基于 極光...
    anyurchao閱讀 2,458評論 3 26
  • 版本記錄 前言 前一篇已經對極光推送產品架構和服務進行了介紹,這一篇則只對ios客戶端的集成和使用等狀況進行了說明...
    刀客傳奇閱讀 1,207評論 0 0
  • 1:在極光開發者服務創建應用應用名稱應用圖標APNS開發證書(p12文件)(開發證書密碼:-安裝開發者證書到鑰匙串...
    3ad997c871e9閱讀 856評論 0 0