iOS 容联离线消息推送

 

 

容联 SDK 是包含离线消息推送的。首先我们要在控制台上传P12证书,(生产证书和开发证书),(证书的配置这里就不多说了)只能有一个证书是生效中的

 

 

生产证书需要打包成 ipa 包才能测试离线推送

然后在看下代码中的配置

 

注册 APNS

/** 注册远程通知 */
- (void)registerRemoteNotification {

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [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] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
    } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
                                                                       UIRemoteNotificationTypeSound |
                                                                       UIRemoteNotificationTypeBadge);
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
    }
}
#pragma mark - APP运行中接收到通知(推送)处理

/** APP已经接收到“远程”通知(推送) - (App运行在后台/App运行在前台)  */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    

    /////////下面是 IM
    self.callid = nil;
    NSString *userdata = [userInfo objectForKey:@"c"];
    NSLog(@"远程推送userdata:%@",userdata);
    if (userdata) {
        NSDictionary*callidobj = [NSJSONSerialization JSONObjectWithData:[userdata dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"远程推送callidobj:%@",callidobj);
        if ([callidobj isKindOfClass:[NSDictionary class]]) {
            self.callid = [callidobj objectForKey:@"callid"];
        }
    }
    
    NSLog(@"远程推送 callid=%@",self.callid);
}

角标的显示

- (void)applicationWillResignActive:(UIApplication *)application {
    
    NSInteger count = 0;
    UINavigationController * rootView = (UINavigationController*)self.window.rootViewController;
    if ([rootView.viewControllers[0] isKindOfClass:[MainViewController class]]) {
        count = [[IMMsgDBAccess sharedInstance] getUnreadMessageCountFromSession];
    }
    application.applicationIconBadgeNumber = count;
    
    //离线消息的角标数显示
    [[ECDevice sharedInstance] setAppleBadgeNumber:count completion:^(ECError *error) {
        
    }];
    usleep(10);
}

注意,在容联登录的地方要设置对应的环境.不设置默认代表是生产环境要对应生产环境的证书

-(void)resetResourceServer {

    

    [self setAppKey:DefaultAppKey AndAppToken:DefaultAppToke];

    

    //Caches文件路径

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    //服务器配置文件夹

    NSString * config = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"config.data"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:config]) {

        [[NSFileManager defaultManager] removeItemAtPath:config error:nil];

    }

    

#ifdef DEBUG

    [[ECDevice sharedInstance] SwitchServerEvn:YES];

#else

    [[ECDevice sharedInstance] SwitchServerEvn:NO];

#endif

}

 

posted @ 2017-03-22 15:21  _水畔竹汐  阅读(725)  评论(0编辑  收藏  举报