个推集成

作者: 咔咔尼亜 | 来源:发表于2019-07-01 15:07 被阅读0次

个推集成

第一步

通过个推的官网下载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,由于这种情况的存在,因此上如果想要在任何时候都有顶部弹窗的话,可以在透传消息代理回调中创建一个本地通知并弹出来。

相关文章

  • iOS开发个推集成中的注意点(手动集成)

    一、个推的集成 1.个推的集成可以参考个推的集成文档。 2.初步集成的代码如下: APPDelegate.h AP...

  • 个推集成

    集成过程就不多说了,参照官方文档。(消息推送必须用真机测试)官方地址:个推官网 集成大致过程: 1.登陆注册 2....

  • 个推集成

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

  • IOS集成个推总结

    IOS集成个推总结 标签(空格分隔): IOS IOS集成个推总结 集成注意点 1 去个推后台申请app的应用 如...

  • 个推 集成过程

    当初选择 它,估计是表面上看它太简单了,集成只需要几分钟,结果,并不是那么如意,记录下问题首先,注册账号,打开官网...

  • iOS 个推集成 <笔记篇>

    参考个推集成方式① iOS集成视频[https://docs.getui.com/getui/mobile/ios...

  • 个推的集成使用

    为应用创建 APNs 推送证书,可以按照个推说明的去配置证书和描述文件 证书配置 描述文件配置 到个推注册应用取得...

  • 个推集成注意点

    1.导入推送证书时显示“证书存在多个bundleID” 这是因为在keychain Access里导出p12文件时...

  • 个推SDK的集成

    关于个推 实时推送的一款SDK,比较好用,稳定 如何把个推SDK集成到你的APP里 当然看SDK还是王道 我写一下...

  • iOS集成个推小结

    一、以下为我集成时的问题与解决方案: 1、iOS通知,在官网后台怎么推送,不能单独推送通知? iOS只能透传消息,...

网友评论

    本文标题:个推集成

    本文链接:https://www.haomeiwen.com/subject/upigcctx.html