关于远程推送,设置别名的一些坑(傻瓜教学)

记点东西,关于远程推送的
远程推送这个东西不多,但是如果本地服务器写的话还是很麻烦的,因为如果实现单推,还要实现很多的表绑定变换什么的,所以主流现在一般都用的第三方,基本不收费,而且也很简单

我这里用的#####极光推送
记录一些坑吧,以后方便用,也方便大家用

代码很简单,就是把他的官方文档里面的代码复制下来就行了...

下面所有代码是照抄的

在你的下面这个方法里加上通知部分,直接复制就行了

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark - 通知
//    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    //Required
    // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。

// 下面这个是老方法,需要加plist,现在不用了,用下面的方法,这个设置一遍就行了
// 把这个appKey换成你自己的appKey就行了,其他直接复制
//    [JPUSHService setupWithOption:launchOptions];
    [JPUSHService setupWithOption:launchOptions
                           appKey:@"a59c6b6037300edb93384d55"
                          channel:@"Publish channel"
                 apsForProduction:NO];
 return YES;
}


#pragma mark - 通知
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}

// 后台更新的方法,实现这个方法必须开启后台模式,一般情况下不用,注释了就行了
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//    
//    // IOS 7 Support Required
//    [JPUSHService handleRemoteNotification:userInfo];
//    completionHandler(UIBackgroundFetchResultNewData);
//}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

第一个坑,就是下面这个代码

正常情况下,你外部的APP ICON的角标是固定为1的,你就是推送1000条,也是1.
这个时候我本来想是说在接收到通知的时候,自己用一个变量记录+1,然后用setApplicationIconBadgeNumber设置角标,但是仔细想想是不可行的,程序不运行,你是实现不了+1操作的.
所以~~~这个玩意儿是后端做的,后端发了通知,然后改角标,去告诉苹果服务器,我角标变了,苹果服务器告诉你直接改了,跟你没啥关系
但是,如果你要实现点击进入APP,角标清空,就必须在下面方法里面实现我写的两个方法,一个是让角标清空,一个是让通知栏清空,ok,就是这样,瞄~

- (void)applicationWillResignActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

好了,如果你单单想实现群推,比如,发个什么广告了,这种大家不区分用户的情况,这样就够了

但是,如果你想区分用户单推,比如QQ消息这种,你就得设置唯一标示,也就是别名
这个时候需要注意两点,

1, 在登录成功的时候设置别名(一般就是userID什么的唯一标示)

2.在推出登录的时候把别名取消,否则及时推出也能收到推送,就不安全了

代码很简单,如下

// 登录成功
#pragma mark - 推送别名设置
        [JPUSHService setTags:nil alias:userID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            XHLog(@"%d-------------%@,-------------%@",iResCode,iTags,iAlias);
        }];

// 退出登录
#pragma mark - 推送,用户退出,别名去掉
        
        [JPUSHService setAlias:@"" callbackSelector:nil object:self];

好了,说俩坑

第一,描述证书一定要导清楚,不然出现乱七八糟的问题

1.会报错APNs is not available,please check your provisioning profile and certificatio
2.你会发现你设置的开发环境跟测试环境完全没有卵用,而且乱七八糟的表现...我这边5测试机在开发环境无限收到推送信息,完全停不下来,6sp的完全收不到,但是在生产环境下完全没有问题,推得好好地...
3.你实现的单推完全没有作用

第二,别名设置不上,控制台打印信息app not registed, give up set tag:

感觉是由于推送服务还没接通就去设置了,所以设置的别名被放弃了。
这个看好像有很多状态吗设置,不过没具体去弄,百度了下用的延迟操作,代码如下

#pragma mark - 推送别名设置
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [JPUSHService setTags:nil alias:userID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            XHLog(@"%d-------------%@,-------------%@",iResCode,iTags,iAlias);
        }];
    });

注意一条,我建议用这个回调的方法直接打印状态码来调试,这样很方便的能找到问题,恕我直言...我没搞明白那个回调的方法里面怎么传参数...

设置tag就是群体设置,也是区分用户用的,比如说性别男女什么的,跟别名一个效果就不单独说了

好了,就是这样,瞄~

你可能感兴趣的:(关于远程推送,设置别名的一些坑(傻瓜教学))