zoukankan      html  css  js  c++  java
  • iOS 集成极光推送

    最近极光推送更新到V3版本之后,推送又不成功!配合服务器联调了半天,发现是服务器环境配置有问题。

    想着就把极光推送的步骤给记录下来。

    一、配置push证书

    这个可以到极光文档里写,很详细

    二、导入必要的框架

    CFNetwork.framework
    CoreFoundation.framework
    CoreTelephony.framework
    SystemConfiguration.framework
    CoreGraphics.framework
    Foundation.framework
    UIKit.framework
    Security.framework
    Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib

    三、代码中注册极光推送

    首先在AppDelegate.m 导入#import "JPUSHService.h"
    在~didFinishLaunchingWithOptions方法贴上核心代码:

    // 1.注册系统通知
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
        [application registerUserNotificationSettings:settings];
    }
     // 2.注册极光推送
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
            JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
            entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
            [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //可以添加自定义categories
            [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                              UIUserNotificationTypeSound |
                                                              UIUserNotificationTypeAlert)
                                                  categories:nil];
        }
        [JPUSHService setupWithOption:launchOptions appKey:@"注册极光生成的appKey"
                              channel:@"Publish channel"
                     apsForProduction:NO
                advertisingIdentifier:nil];
    
    // 接收应用内消息
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    // 极光推送登录成功后可以注册别名
        [defaultCenter addObserver:self selector:@selector(registerAlias:) name:kJPFNetworkDidLoginNotification object:nil];   
    // 3.注册 DeviceToken
    - (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)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三种类型可以选择设置
        // 通知内容为:notification.request.content.body
    }
    
    // iOS 10 Support,用户点击了通知进入app
    - (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];
        }
        completionHandler();  // 系统要求执行这个方法
    }
    #pragma mark 解析极光推送的应用内消息
    - (void)networkDidReceiveMessage:(NSNotification *)notification {
        NSDictionary * userInfo = [notification userInfo];
        NSLog(@"解析极光推送的应用内消息:%@",userInfo);
    }
    
    // 接收到远程通知之后
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        
        // Required,For systems with less than or equal to iOS6
        [JPUSHService handleRemoteNotification:userInfo];
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        
        // IOS 7 Support Required
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    //获取 deviceToken 失败后 远程推送(极光推送)打开失败
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        //Optional
        NSLog(@"获取 device token 失败 %@", error);
    }

    五、注册别名(可选)

    主要是在项目的登录成功或者自动登录后,使用用户的唯一标示进行绑定

    // 注册别名
    [JPUSHService setAlias:@"一般为用户的账号" callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    
    
    // 极光别名注册的回调方法
    -(void)tagsAliasCallback:(int)iResCode
                        tags:(NSSet*)tags
                       alias:(NSString*)alias
    {
        NSLog(@"极光别名注册的回调方法rescode: %d, 
    tags: %@, 
    alias: %@
    ", iResCode, tags , alias);
        if (iResCode == 0) {
            // 注册成功
        }
    }

    去除绑定

    用户进行退出登录的方法里添加去除绑定即可,值得注意的是用到即时通讯的话,被挤下线也要去除绑定,

    //没有值就代表去除
    [JPUSHService setTags:[NSSet set]callbackSelector:nil object:self];
    [JPUSHService setAlias:@"" callbackSelector:nil object:self];
    [JPUSHService setTags:[NSSet set] alias:@"" callbackSelector:nil target:self];
  • 相关阅读:
    tcp 粘包 和 TCP_NODELAY 学习
    分解抓取的包文件代码实现学习
    谨慎使用多线程中的fork 学习!!!!
    面试题
    Java并发编程:Lock
    为什么匿名内部类参数必须为final类型
    sql 面试题
    java hashCode方法返回值
    数组初始化
    Java内存模型
  • 原文地址:https://www.cnblogs.com/shen5214444887/p/6530160.html
Copyright © 2011-2022 走看看