个推集成

个推集成

第一步

通过个推的官网下载SDK并集成SDK,两种方式,第一种直接拖拽,第二种cocaapods,(略过)

第二步

登录个推SDK,在appDelegate的didFinishLaunchingWithOptions方法中

[GeTuiSdk startSdkWithAppId:GeTuiAppID appKey:GeTuiKey appSecret:GeTuiSecret delegate:self];

第三步

个推SDK注册通知类型。

//注册通知类型
- (void)registerRemoteNotification
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError *_Nullable error) {
            if (!error) {
                NSLog(@"request authorization succeeded!");
            }
        }];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
#else // Xcode 7编译会调用
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#endif
    } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
                                                                       UIRemoteNotificationTypeSound |
                                                                       UIRemoteNotificationTypeBadge);
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(geTuiLogin:) name:@"geTuiLogin" object:nil];
    // 处理app在前台自定义推送弹窗点击事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eBBannerViewDidClick:) name:EBBannerViewDidClick object:nil];
}
注册通知
- (void)registerAPN
{
    if (@available(iOS 10.0, *)) { // iOS10 以上
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError *_Nullable error) {
        }];
    } else {// iOS8.0 以上
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    }
}

第四步

个推SDK注册token,用于唯一识别机器

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // [3]:向个推服务器注册deviceToken 为了方便开发者,建议使用新方法
    [GeTuiSdk registerDeviceTokenData:deviceToken];
}
//个推注册deviceToken成功回调
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId
{
    //个推SDK已注册,返回clientId
    NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
    //绑定别名
    [self geTuiBindOrUnbindAlias:YES];
}

第五步

个推绑定别名,实现绑定成功或失败的回调。

// 绑定解绑别名
- (void)geTuiBindOrUnbindAlias:(BOOL)isBind
{
    UserInfo *name = [HMFileManager getObjectByFileName:@"user"];
    if ([name.carownerCode length] > 0) {
        if (isBind) {
            [GeTuiSdk bindAlias:name.carownerCode andSequenceNum:@"seq-1"];
        } else {
            [GeTuiSdk unbindAlias:name.carownerCode andSequenceNum:@"seq-1" andIsSelf:YES];
        }
    }
}
//绑定成功失败回调
- (void)GeTuiSdkDidAliasAction:(NSString *)action result:(BOOL)isSuccess sequenceNum:(NSString *)aSn error:(NSError *)aError
{
    if ([kGtResponseBindType isEqualToString:action]) {
        if (!isSuccess) {
            NSLog(@"标签绑定失败原因: %@", aError);
        }
    } else if ([kGtResponseUnBindType isEqualToString:action]) {
        if (!isSuccess) {
            NSLog(@"标签解绑失败原因: %@", aError);
        }
    }
}

第六步

实现收到通知或透传消息的代理方法。

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
//  iOS 10: App在前台获取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);
    [self getUnreadMessageNum];
    // 根据APP需要,判断是否要提示用户Badge、Sound、Alert
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

//  iOS 10: 点击通知进入App时触发,在该方法内统计有效用户点击数
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    
    [self allZYUniversalModel:response.notification.request.content.userInfo isRead:@"YES" startUp:@"YES"];
    // [ GTSdk ]:将收到的APNs信息传给个推统计
    [GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];

    completionHandler();
}

#endif


/** SDK收到透传消息回调 */
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId
{
    //收到个推消息
    NSError *error;
    NSDictionary *msgDic = nil;
    if (payloadData) {
        msgDic = [NSJSONSerialization JSONObjectWithData:payloadData options:NSJSONReadingMutableLeaves error:&error];
    }
    NSString *payloadStr = [[NSString alloc]initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding];

    NSMutableDictionary *tempDic = [[NSMutableDictionary alloc]initWithDictionary:msgDic];
    NSString *platformType = msgDic[@"platformType"];
    
    if (![platformType.lowercaseString containsString:@"ios"]) {
        return;
    }
    tempDic[@"_gmid_"] = msgId;
    
    [GeTuiSdk handleRemoteNotification:tempDic];

//    //添加本地通知
    [self addLocalNoticeWith:tempDic];

    NSLog(@"\n>>>[GexinSdk ReceivePayload]:%@\npayloadData==%@\n", msgDic, payloadStr);
}

集成过程中遇到的坑 第一,个推的SDK登录非常慢导致的问题,因为如果app处于前台工作状态,SDK没有登录成功的时候后台发送推送通知,这个时候,通知是会从顶部弹出的(如果设置了顶部弹出框的话)这个时候走的是willPresentNotification代理方法。如果SDK登录成功了,那么收到的将是透传消息,也就是走的是透传消息的代理方法也就是GeTuiSdkDidReceivePayloadData,由于这种情况的存在,因此上如果想要在任何时候都有顶部弹窗的话,可以在透传消息代理回调中创建一个本地通知并弹出来。

你可能感兴趣的:(个推集成)