重要:自 2020 年 6 月 28 日起,mPaaS 停止维护 10.1.32 基线。请使用 10.1.68 或 10.1.60 系列基线。可以参考 mPaaS 10.1.68 升级指南 或 mPaaS 10.1.60 升级指南 进行基线版本升级。 |
本文将向您详细介绍将消息推送服务接入 iOS 客户端的接入流程。为了使用消息推送服务,您需要完成以下流程:
消息推送服务支持以下三种接入方式,您可以根据实际情况进行选择。关于接入方式的更多信息,请参见 接入方式简介。在确定接入方式后,请您参考各接入方式的接入文档,添加消息推送 SDK 完成接入。
需要在工程 target 设置中开启以下两处:
下面对不同接入方式的 SDK 使用进行说明。
PushSDK 在应用启动完成时,会自动请求注册 deviceToken,一般情况下您无需请求注册 deviceToken。但是当特殊情况下(比如启动时有隐私管控,阻止一切网络请求时)您需要在管控授权后,再次触发注册 deviceToken,示例代码如下:
- (void)registerRemoteNotification
{
// 注册推送
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {// 10.0+
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
}];
} else {// 8.0,9.0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
与基于 iOS 原生框架相比,基于 mPaaS 框架的应用的生命周期被 mPaaS 框架接管,应用获取 deviceToken 的回调方法有所不同,代码示例如下:
// import <PushService/PushService.h>
// 在 DTFrameworkInterface 分类中重写如下方法
- (DTFrameworkCallbackResult)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[PushService sharedService] setDeviceToken:deviceToken];
[[PushService sharedService] pushBindWithUserId:@"your userid(需替换)" completion:^(NSException *error) {
}];
return DTFrameworkCallbackResultContinue;
}
- (void)pushUnBindWithUserId:(NSString *)userId completion:(void (^)(NSException *error))completion;
,用于解除设备的 deviceToken 与当前应用的 userId 的绑定。如在用户切换账号后,可以调用解绑接口。基于 mPaaS iOS 框架的应用,由于其生命周期被 mPaaS 框架接管,与基于 iOS 原生框架相比,收到消息的回调方法不同,代码示例如下:
// import <PushService/PushService.h>
// 在 DTFrameworkInterface 分类中重写如下方法
- (DTFrameworkCallbackResult)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// userInfo 为推送消息内容,业务解析处理
return DTFrameworkCallbackResultContinue;
}
在基于已有工程且使用 mPaaS 插件或 CocoaPods 接入 iOS 客户端的情况下,您需要完成以下操作。
PushSDK 在应用启动完成时,会自动请求注册 deviceToken,一般情况下您无需请求注册 deviceToken。但是当特殊情况下(比如启动时有隐私管控,阻止一切网络请求时)您需要在管控授权后,再次触发注册 deviceToken,示例代码如下:
- (void)registerRemoteNotification
{
// 注册推送
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {// 10.0+
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
}];
} else {// 8.0,9.0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
mPaaS 提供的 Push SDK 中封装了向 APNs 服务器注册的逻辑,在程序启动后,Push SDK 自动向 APNs 服务器注册。您可在注册成功的回调方法中获取 APNs 下发的 DeviceToken,然后调用 PushService
的接口方法,上报绑定 userId 至移动推送核心。
// import <PushService/PushService.h>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[PushService sharedService] setDeviceToken:deviceToken];
[[PushService sharedService] pushBindWithUserId:@"your userid(需替换)" completion:^(NSException *error) {
}];
}
- (void)pushUnBindWithUserId:(NSString *)userId completion:(void (^)(NSException *error))completion;
,用于解除设备的 deviceToken 与当前应用的 userId 的绑定。如在用户切换账号后,可以调用解绑接口。客户端收到 push 消息后,如果用户点击查看,系统将启动相应应用。可在 AppDelegate
的回调方法中完成收到 push 消息后的逻辑处理。
在 iOS 10 以下系统中,通知栏消息或静默消息的处理方法如下:
// iOS 10 以下 Push 冷启动处理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 10.0) {
// iOS 10 以下 Push 冷启动处理
}
return YES;
}
// App 在前台时,普通推送的处理方法;App 在前台或后台时,静默推送的处理方法;iOS 10 以下系统,通知栏消息处理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
//处理接受到的消息
}
// 注册 UNUserNotificationCenter delegate
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 10.0) {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
}
//应用处于前台时的远程推送接受
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSDictionary *userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
} else {
//应用处于前台时的本地推送接受
}
completionHandler(UNNotificationPresentationOptionNone);
}
//应用处于后台或者活冷启动时远程推送接受
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
{
NSDictionary *userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台或者活冷启动时远程推送接受
} else {
//应用处于前台时的本地推送接受
}
completionHandler();
}
要使用 mPaaS 消息推送控制台推送消息,您需要在控制台中配置 APNs 推送证书。该证书必须是与客户端签名对应的推送证书,否则客户端会收不到推送消息。
有关详细的配置说明,查看 iOS 推送证书配置。
点击此处 下载示例代码包。
支持的推送类型如下:
关于各推送类型的定义,参见 推送类型。
在文档使用中是否遇到以下问题
更多建议
匿名提交