极光推送流程

1、使用cocoaPods导入,在终端输入 *** pod search JPush***,习惯先搜索pod中有没有。
2、在podfile文件中添加 pod 'JPush'
3、导入极光SDK pod install --verbose --no-repo-update
4、极光后台注册项目名称,上传两个推送用证书(一个开发用,一个发布用)
5、在Appdelegate中导入头文件 #import "JPUSHService.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //极光推送
    [self createJPush:launchOptions];
    
    return YES;
}

//极光推送
- (void)createJPush:(NSDictionary *)launchOptions {
    
    if ([UIDevice currentDevice].systemVersion.floatValue>=8.0) {
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    } else {
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
    }
    [JPUSHService setupWithOption:launchOptions appKey:@"JPUSH_KEY" channel:@"AppStore" apsForProduction:NO];

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"获取到了deviceToken:%@",deviceToken);
    [JPUSHService registerDeviceToken:deviceToken];
}


-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"呵呵-->%@",userInfo);
    /**
     *  正在运行,推送的消息
     */
    UIAlertController *ale = [UIAlertController alertControllerWithTitle:@"消息" message:userInfo[@"aps"][@"alert"] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ac1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [ale addAction:ac1];
    [_navigationController presentViewController:ale animated:YES completion:nil];
    
    [JPUSHService handleRemoteNotification:userInfo];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
    /**
     *  正在运行,推送的消息
     */
    UIAlertController *ale = [UIAlertController alertControllerWithTitle:@"消息" message:userInfo[@"aps"][@"alert"] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ac1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [ale addAction:ac1];
    [_navigationController presentViewController:ale animated:YES completion:nil];
        
    NSLog(@"哈哈-->%@",userInfo);
    /**
     *  极光处理消息是否被点击什么的
     */
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"注册推送失败:%@",error);
}

6、在项目的组织文件中设置

极光推送流程_第1张图片
BA2CBE5E-1C4A-47DC-BC23-11482DE58760.png

7、推送消息时,这里要设置一下可以让图标上未读消息的数字加1

极光推送流程_第2张图片
FB3176DB-938C-4ABE-ADE2-FB2605F7FB1A.png

8、图标上未读消息的数字清零

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    //即将进入前台,图标上的未读消息清零
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

你可能感兴趣的:(极光推送流程)