本人因公司最近要求需要集成个推推送,现将个推从集成到成功集成推送流出中遇到的问题写出来,做个笔记,同时也希望帮助其他的遇到问题的人#
这里装个逼哈 引用一段经典名言#
痛苦留给的一切,请细加回味!苦难一经过去,苦难就变为甘美。 ——歌德
废话不说了 上图片代码
首先按照个推官网的详细文档集成到项目中,包括导一些依赖库等,这里不细说了。这一步一半都没啥问题
#import "GeTuiSdk.h"
@interface AppDelegate ()<UITabBarControllerDelegate,UIScrollViewDelegate,NetWebServiceRequestDelegate,UIApplicationDelegate, GeTuiSdkDelegate,UNUserNotificationCenterDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [1]:使用APPID/APPKEY/APPSECRENT创建个推实例
//这里需要再个推官网申请的kAppId kAppKey kAppSecret
[GeTuiSdk startSdkWithAppId:kAppId appKey:kAppKey appSecret:kAppSecret
delegate:self];
[self registerRemoteNotification];
[GeTuiSdk runBackgroundEnable:true];
[GeTuiSdk setChannelId:@"GT-Channel"];
//重置角标计数
//重置角标代码本人是添加到整个工程项目中 只要打开程序 角标就置为0 这是比较笨的方法 目前没有最好方法 就先这样 希望大家看完有啥好方法可以附评价给我
page=0;
[GeTuiSdk resetBadge];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:page];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [UMSocialSnsService handleOpenURL:url];
}
/** 远程通知注册成功委托 */
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
// UIRemoteNotificationType t = UIRemoteNotificationTypeNone|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;
//
// [[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];
NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);
DeviceToken = token;
// deviceToken
[GeTuiSdk registerDeviceToken:token];
}
/** 远程通知注册失败委托 */
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description);
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
/// Background Fetch SDK
[GeTuiSdk resume];
completionHandler(UIBackgroundFetchResultNewData);
}
/** APP已经接收到“远程”通知(推送) - 透传推送消息 */
//处理APNs展示点击,统计有效用户点击数
// 处理APNs代码,通过userInfo可以取到推送的信息(包括内容,角标,自定义参数等)。如果需要弹窗等其他操作,则需要自行编码。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
page+=1;
[GeTuiSdk setBadge:page];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:page]; //可用全局变量累加消息
// 将收到的APNs信息传给个推统计
[GeTuiSdk handleRemoteNotification:userInfo];
NSLog(@"\n>>>[Receive RemoteNotification - Background Fetch]:%@\n\n",userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - GeTuiSdkdelegate 注册回调,获取CID信息
/** SDK 启动成功返回 cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
// SDK clientId
NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
adduserID = clientId;
[self adduserId];
}
/** SDK 遇到错误回调 */
- (void)GeTuiSdkDidOccurError:(NSError *)error {
//
NSLog(@"\n>>>[GexinSdk error]:%@\n\n", [error localizedDescription]);
}
//个推透传消息通道---在线
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
// [4]: 收到个推消息
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];
NSString *payLoadMsgIndexone = [[NSString alloc]init];
payLoadMsgIndexone = [payloadMsg substringToIndex:2];
// NSLog(@"%@",payLoadMsgIndexone);
//判断跳转页面的值
NSDictionary *dic3 =[NSDictionary dictionaryWithObject:payLoadMsgIndexone forKey:@"message2"];//
//
[[NSNotificationCenter defaultCenter]postNotificationName:@"Message2" object:nil userInfo:dic3];
NSString *payLoadMsgIndexone2 = [[NSString alloc]init];
payLoadMsgIndexone2 = [payloadMsg substringFromIndex:2];
NSDictionary *dic2 =[NSDictionary dictionaryWithObject:payLoadMsgIndexone2 forKey:@"message"];//
//
[[NSNotificationCenter defaultCenter]postNotificationName:@"Message" object:nil userInfo:dic2];
}
#pragma mark - APP运行中接收到通知(推送)处理
//====================For iOS 10====================
#这俩个方法是我自己做iOS适配的 可以不用添加
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"Userinfo %@",notification.request.content.userInfo);
//功能:可设置是否在应用内弹出通知
completionHandler(UNNotificationPresentationOptionAlert);
}
//点击推送消息后回调
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
- (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);
}
}
}