10分钟集成iOS极光推送SDK

2017-09-18  本文已影响0人  红先生的小简书

配置:

  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 {
   // 此处用于处理点击消息跳转界面的效果
}
上一篇 下一篇

猜你喜欢

热点阅读