iOS 容联离线消息推送
2024-10-19 13:34:52
容联 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 = ;
UINavigationController * rootView = (UINavigationController*)self.window.rootViewController;
if ([rootView.viewControllers[] isKindOfClass:[MainViewController class]]) {
count = [[IMMsgDBAccess sharedInstance] getUnreadMessageCountFromSession];
}
application.applicationIconBadgeNumber = count; //离线消息的角标数显示
[[ECDevice sharedInstance] setAppleBadgeNumber:count completion:^(ECError *error) { }];
usleep();
}
注意,在容联登录的地方要设置对应的环境.不设置默认代表是生产环境要对应生产环境的证书
-(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
}
最新文章
- getchar fflush 的分析笔记
- bootstrap学习笔记系列4------bootstrap按钮
- 字符集GBK升级UTF8
- 直接用Qt写soap
- XE3随笔8:关于乱码
- 反转字符串--C和Python
- 解决数据标准及质量AppScan(测试)安全性问题相关方法
- [Effective C++ --010]令赋值操作符返回一个reference to *this
- stream的Read、Write方法实例
- block 和delegate的用法
- asp.net [AjaxMethod]
- 苹果新的编程语言 Swift 语言进阶(十二)--选项链
- Error while registering Oracle JDBC Diagnosabilityh
- OrientDB入门(1)Getting Started
- 几个常用内核函数(《Windows内核情景分析》)
- 微信小程序中的组件使用1
- Ubuntu18.04提示wifi无法连接
- textwrap 笔记
- STM8S——8位基本定时器(TIM4)
- JS调试必备的5个debug技巧_javascript技巧
热门文章
- BZOJ_3124_[Sdoi2013]直径_树形DP
- 【LeetCode】042 Trapping Rain Water
- pycharm+QT5+python3安装与环境配置
- 洛谷【P3960】列队
- Windows WMIC命令使用详解2
- ie8兼容rgba的方法
- [poj1222]EXTENDED LIGHTS OUT(高斯消元)
- Bind 远程连接出现rndc: connect failed: 192.168.1.66#953: connection refused
- Android图片压缩框架-Tiny 集成
- Unity中场景异步加载