SDK使用流程
所有文档
menu
没有找到结果,请重新输入

移动APP推送服务

SDK使用流程

业务方使用Xcode把推送SDK集成好后,建议按照如下流程进行代码集成调用。涉及到的推送SDK相关接口,在后续SDK接口介绍章节中详细说明。

启动推送SDK并注册APNs

#import "SPSDKLib.h" // 引入头文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [SPSDKLib startSDKWithAppkey:@"appkey" secretKey:@"secretKey" CUID:@"cuid"]; // 启动SDK
    [self registerAPNs]; // 注册APNs
}

// 注册APNs,针对不同iOS版本采取不同注册方式,以下为举例代码
- (void)registerAPNs {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
            // 请根据error自行处理 
        }];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        UIRemoteNotificationType types = (UIRemoteNotificationType)
        (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge);
        [[UIApplication sharedApplication]
        registerForRemoteNotificationTypes:types];
    } 
}

向推送服务器注册DeviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //Xcode11打的包,iOS13获取Token有变化
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        if (![deviceToken isKindOfClass:[NSData class]]) {
            //记录获取token失败的描述
            return;
        }
        const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
        NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        NSLog(@"deviceToken1:%@", strToken);
        [SPSDKLib registerDeviceToken:strToken withCompletion:^(NSError *error) {
            if (!error) {
                NSLog(@"注册设备ID成功");
            } else {
                NSLog(@"error code : %@", error);
            }
        }];
    } else {
        NSString *token = [NSString
                       stringWithFormat:@"%@",deviceToken];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"deviceToken2 is: %@", token);
        [SPSDKLib registerDeviceToken:token withCompletion:^(NSError *error) {
            if (!error) {
                NSLog(@"注册设备ID成功");
            } else {
                NSLog(@"error code : %@", error);
            }
        }];
    }
}

上报收到APNs推送事件

需要在AppDelegate.m中接收到远程推送消息的代理方法中执行,根据iOS版本有所区分。

  • iOS10以前处理方式:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [SPSDKLib didRevRemoteNotification:userInfo applicationState:application.applicationState];
}  
  • iOS10及以后处理方式:
// App前台运行时触发
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary * userInfo = notification.request.content.userInfo;
    [SPSDKLib didRevRemoteNotification:userInfo applicationState:[[UIApplication sharedApplication] applicationState]];
}

// 点击通知消息,App进入前台处理时触发
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    [SPSDKLib didRevRemoteNotification:userInfo applicationState:[[UIApplication sharedApplication] applicationState]];
}
    
上一篇
项目工程集成SDK
下一篇
SDK接口介绍及示例