个推的集成使用

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

证书配置

描述文件配置

到个推注册应用取得AppId,AppKey,AppSecret,应用注册号之后不要立即使用,等上10分钟后使用

开始集成,导入sdk及所需要的库,然后是打开推送功能

开启 Target -> Capabilities -> Push Notifications。如果没有开启,在 Xcode8 上编译中将获取不到 DeviceToken。


SDK后台运行权限设置

在AppDelegate 中注册 GeTuiSdkDelegate

// iOS10 及以上需导入 UserNotifications.framework

#import<UserNotifications/UserNotifications.h>

/// 个推开发者网站中申请App时,注册的AppId、AppKey、AppSecret

#define kGtAppId          @"iMahVVxurw6BNr7XSn9EF2"  

#define kGtAppKey          @"yIPfqwq6OMAPp6dkqgLpG5"

#define kGtAppSecret      @"G0aBqAD6t79JfzTB6Z5lo5"

/// 需要使用个推回调时,需要添加"GeTuiSdkDelegate" /// iOS 10 及以上,需要添加 UNUserNotificationCenterDelegate 协议,才能使用 UserNotifications.framework 的回调 @interface AppDelegate : UIResponder<UIApplicationDelegate, GeTuiSdkDelegate, UNUserNotificationCenterDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 通过个推平台分配的appId、 appKey 、appSecret 启动SDK,注:该方法需要在主线程中调用

[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];

// 注册 APNs

[self registerRemoteNotification];

return YES;

}

/** 注册 APNs */

- (void)registerRemoteNotification {

/*

警告:Xcode8 的需要手动开启“TARGETS -> Capabilities -> Push Notifications”

*/

/*

警告:该方法需要开发者自定义,以下代码根据 APP 支持的 iOS 系统不同,代码可以对应修改。

以下为演示代码,注意根据实际需要修改,注意测试支持的 iOS 系统都能获取到 DeviceToken

*/

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 | UNAuthorizationOptionCarPlay) 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];

}

}

向个推服务器注册DeviceToken

/** 远程通知注册成功委托 */

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);

//向个推服务器注册deviceToken

[GeTuiSdk registerDeviceToken:token];

}

GeTuiSdk注册回调,获取CID信息

/** SDK启动成功返回cid */

- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {

//个推SDK已注册,返回clientId

NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);

}

/** SDK遇到错误回调 */

- (void)GeTuiSdkDidOccurError:(NSError *)error {

//个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。

NSLog(@"\n>>>[GexinSdk error]:%@\n\n", [error localizedDescription]);

}

iOS 10 中,处理 APNs 展示点击,统计有效用户点击数,需先添加 UNUserNotificationCenterDelegate 后,在 AppDelegate.m 实现的 didReceiveNotificationResponse 方法中调用处理方法:

//  iOS 10: App在前台获取到通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);

// 根据APP需要,判断是否要提示用户Badge、Sound、Alert

completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);

}

//  iOS 10: 点击通知进入App时触发,在该方法内统计有效用户点击数

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);

// [ GTSdk ]:将收到的APNs信息传给个推统计

[GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];

completionHandler();

}

设置别名,别名推送

// 绑定别名

[GeTuiSdk bindAlias:@"个推" andSequenceNum:@"seq-1"];

// 取消绑定别名

[GeTuiSdk unbindAlias:@"个推" andSequenceNum:@"seq-2"];

处理 绑定/解绑 返回:

- (void)GeTuiSdkDidAliasAction:(NSString *)action result:(BOOL)isSuccess sequenceNum:(NSString *)aSn error:(NSError *)aError {

//  解绑别名必须在账号退出之前解绑

if ([kGtResponseBindType isEqualToString:action]) {

NSLog(@"绑定结果 :%@ !, sn : %@", isSuccess ? @"成功" : @"失败", aSn);

if (!isSuccess) {

NSLog(@"失败原因: %@", aError);

}

} else if ([kGtResponseUnBindType isEqualToString:action]) {

NSLog(@"绑定结果 :%@ !, sn : %@", isSuccess ? @"成功" : @"失败", aSn);

if (!isSuccess) {

NSLog(@"失败原因: %@", aError);

}

}

}

使用个推 SDK 透传消息, 由个推通道下发 (非 APNs)

//  这个感觉就是个推比别的推送好用的地方了,只要clientID在线就可以直接聊天,类似与及时通讯

/** SDK收到透传消息回调 */

- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {

//收到个推消息

NSString *payloadMsg = nil;

if (payloadData) {

payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes

length:payloadData.length

encoding:NSUTF8StringEncoding];

}

NSString *msg = [NSString stringWithFormat:@"taskId=%@,messageId:%@,payloadMsg:%@%@",taskId,msgId, payloadMsg,offLine ? @"<离线消息>" : @""];

NSLog(@"\n>>>[GexinSdk ReceivePayload]:%@\n\n", msg);

[GeTuiSdk sendFeedbackMessage:90001 andTaskId:taskId andMsgId:msgId];

if (offLine) {    //  离线消息已经有苹果的apns推过消息了,避免上线后再次受到消息

return;

}

//  处理应用在线收到的消息

}

苹果官方静默推送

/** APP已经接收到“远程”通知(推送) - 透传推送消息  */

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {

// 处理APNs代码,通过userInfo可以取到推送的信息(包括内容,角标,自定义参数等)。如果需要弹窗等其他操作,则需要自行编码。

NSLog(@"\n>>>[Receive RemoteNotification - Background Fetch]:%@\n\n",userInfo);

completionHandler(UIBackgroundFetchResultNewData);

//  处理应用不在线时受到的消息,用户点击推送消息后的跳转

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 许多集成的步骤个推官网都有了,这里只写关于推送的远程推送和本地通知的步骤和代码。APP在后台时:走苹果的APNS通...
    AllureJM阅读 7,866评论 1 9
  • 不同版本极光推送SDK集成各有差异,集成时一定要注意版本号,楼主已将博文更新成最新的SDK JPush v3.0....
    i顺颂时宜阅读 12,384评论 37 170
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,725评论 10 16
  • 推送通知注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不可见的...
    醉叶惜秋阅读 5,442评论 0 3
  • 今天下午见了远远 很开心 希望我们都像当初那样,是一个能把梦想实现的少年。 我在香港过得好不好呢?多亏你也来香港,...
    js92阅读 2,325评论 0 0

友情链接更多精彩内容