移动应用推送 SDK

更新时间:2018-08-14 09:42:05

概述

移动推送是基于大数据技术的移动云服务。帮助App快速集成移动推送的功能,在实现高效、精确、实时的移动推送的同时,极大地降低了开发成本。让开发者最有效地与用户保持连接,从而提高用户活跃度、提高应用的留存率。

依赖 SDK 概述
API 通道 提供API通道能力,和基础环境配置信息。

配置服务

本 SDK 涉及的功能依赖移动应用推送服务,需要先在控制台配置,方才可以使用。

如何配置服务请参见:移动应用推送服务配置

iOS 应用推送还需配置开发环境/生产环境推送证书,如何创建推送证书请参见:iOS推送证书设置

初始化

移动应用推送 SDK 的初始化依赖 API 通道的初始化,请参见:API 通道 SDK - 初始化
在 API 通道初始化成功后,再参考下面的示例代码初始化移动应用推送 SDK:

#import <CloudPushSDK/CloudPushSDK.h>
#import <IMSApiClient/IMSConfiguration.h> 

- (void)initCloudPush {   
    //authCode设置 
    [CloudPushSDK setAuthCode:[[IMSConfiguration sharedInstance] authCode]];
    // 基于无线保镖初始化
    [CloudPushSDK asyncInitWithSecurity:^(CloudPushCallbackResult *res) {
        if (res.success) {
            NSLog(@"Push SDK init with security box success, deviceId: %@", [CloudPushSDK
 getDeviceId]);
         } else {
            NSLog(@"Push SDK init with security box failed, error: %@", res.error); }
         }];
}

使用说明

  • 向苹果 APNs 注册获取 deviceToken 并上报到阿里云推送服务器;
/**
 *    注册苹果推送,获取deviceToken用于推送
 *
 *    @param     application
 */

- (void)registerAPNS:(UIApplication *)application {
      [application registerUserNotificationSettings:
       [UIUserNotificationSettings settingsForTypes:
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                         categories:nil]];
      [application registerForRemoteNotifications];
}
/*
 *  苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器
 */

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
        if (res.success) {
            NSLog(@"Register deviceToken success.");
        } else {
            NSLog(@"Register deviceToken failed, error: %@", res.error);
        }
    }];
}

/*
 *  苹果推送注册失败回调
 */

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
}
  • 推送通知到来监听:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 点击通知将App从关闭状态启动时,将通知打开回执上报
    // [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1)
    [CloudPushSDK sendNotificationAck:launchOptions];
    return YES;
}

/*
 *  App处于启动状态时,通知打开回调
 */

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
    NSLog(@"Receive one notification.");
    // 取得APNS通知内容
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    // 内容
    NSString *content = [aps valueForKey:@"alert"];
    // badge数量
    NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
    // 播放声音
    NSString *sound = [aps valueForKey:@"sound"];
    // 取得Extras字段内容
    NSString *Extras = [userInfo valueForKey:@"Extras"]; //服务端中Extras字段,key是自己定义的
    NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras);
    // iOS badge 清0
    application.applicationIconBadgeNumber = 0;
    // 通知打开回执上报
    // [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1)
    [CloudPushSDK sendNotificationAck:userInfo];
}
  • 关联移动推送到某账号,参考API服务:/uc/bindPushChannel

  • 取消关联移动推送到某账号,参考API服务:/uc/unbindPushChannel

#import <IMSApiClient/IMSApiClient.h>
#import <IMSAuthentication/IMSAuthentication.h>

#pragma mark -

- (void)bindAPNSChannelWithDeviceId:(NSString *)deviceId
                  completionHandler:(void (^)(NSError *error))completionHandler {
    NSString *path = @"/uc/bindPushChannel";
    NSString *version = @"1.0.0";
    NSDictionary *params = @{
                             @"deviceId": deviceId ? : @"",
                             };

    [self requestWithPath:path
                  version:version
                   params:params
        completionHandler:^(NSError *error, id data) {
            if (completionHandler) {
                completionHandler(error);
            }
        }];
}


- (void)unbindAPNSChannelWithDeviceId:(NSString *)deviceId
                    completionHandler:(void (^)(NSError *error))completionHandler {
    NSString *path = @"/uc/unbindPushChannel";
    NSString *version = @"1.0.0";
    NSDictionary *params = @{
                             @"deviceId": deviceId ? : @"",
                             };

    [self requestWithPath:path
                  version:version
                   params:params
        completionHandler:^(NSError *error, id data) {
            if (completionHandler) {
                completionHandler(error);
            }
    }];
}


#pragma mark -

- (void)requestWithPath:(NSString *)path
                version:(NSString *)version
                 params:(NSDictionary *)params
      completionHandler:(void (^)(NSError *error, id data))completionHandler {
    IMSIoTRequestBuilder *builder = [[IMSIoTRequestBuilder alloc] initWithPath:path apiVersion:version params:params];
    IMSRequest *request = [[builder setAuthenticationType:IMSAuthenticationTypeIoT] build];

    [IMSRequestClient asyncSendRequest:request responseHandler:^(NSError *error, IMSResponse *response) {
        if (error == nil && response.code != 200) {
            NSDictionary *info = @{
                                   @"message" : response.message ? : @"",
                                   NSLocalizedDescriptionKey : response.localizedMsg ? : @"",
                                   };
            error = [NSError errorWithDomain:ServerErrorDomain code:response.code userInfo:info];
        }

        if (completionHandler) {
            completionHandler(error, response.data);
        }
    }];
}

results matching ""

    No results matching ""