Commit bdb38571 by zhangyunjie

推送

parent 77d1281e
......@@ -39,7 +39,7 @@
//获取用户相关数据库,该方法在登陆或者第一次启动已登陆下执行
[[OffcnIMZYJFMDBHandler sharedInstance] getUserDB];
//推送
[self registerOffcnUserNotificationSettingsWithApplication:application options:launchOptions];
[AppDelegate registerOffcnUserNotificationSettingsWithApplication:application options:launchOptions];
//获取网络状态和内部初始化处理
[[OffcnIMSDKiOS defaultService] startNotifierUseRealReachability];
......@@ -78,11 +78,11 @@
[[OffcnIMSocketManager defaultService] disConnectIMServer];
}
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler{
}
/**
* 远程推送注册成功
*
......@@ -92,7 +92,7 @@
// 25bb75ac 3ffcebd7 90d9f517 1ebca904 154a367a 87781e5d b9ea288e 37fdf487
NSLog(@"-----deviceToken ---- ---- %@ devDesc ---%@", deviceToken,deviceToken.description);
NSString *str = [self hexStringForData:deviceToken];
NSString *str = [AppDelegate hexStringForData:deviceToken];
[OffcnIMSDKiOS defaultService].deviceToken = str;
NSLog(@"token--- %@",str);
//注册成功,返回token
......@@ -118,7 +118,7 @@
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"接收到远程推送通知Background ---- %@", userInfo);
[self didReceiveRemoteNotification:userInfo];
[AppDelegate didReceiveRemoteNotification:userInfo];
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
// [self pushNotificationDefaultController];
......@@ -130,7 +130,7 @@
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"接收到远程推送通知 ---- %@", userInfo);
[self didReceiveRemoteNotification:userInfo];
[AppDelegate didReceiveRemoteNotification:userInfo];
/*
"message" : "您的车辆京KKKKKK于2016-4-22 驶入邯郸",
......
//
//
......@@ -7,19 +7,23 @@
//
#import "AppDelegate.h"
#import "OffcnIMModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface AppDelegate (OffcnIMPush)
//注册通知
-(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions;
+(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions;
//解析deviceToken
-(NSString *)hexStringForData:(NSData *)data;
+(NSString *)hexStringForData:(NSData *)data;
//收到通知
-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo;
+(void)didReceiveRemoteNotification:(NSDictionary *)userInfo;
//后台收到消息本地推送
+(void)manualSendLocalNotificationWithModel:(OffcnIMModel *)model;
@end
NS_ASSUME_NONNULL_END
......
//
//
......@@ -12,10 +12,11 @@
#import "DataSafe.h"
#import "OffcnIMZYJFMDBHandler.h"
#import "OffcnIMSocketManager.h"
#import <UIKit/UIKit.h>
@implementation AppDelegate (OffcnIMPush)
-(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions{
+(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions{
// application.applicationIconBadgeNumber = 0;
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
......@@ -28,11 +29,16 @@
}
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
// 跳转
NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteUserInfo) {
NSLog(@"remoteUserInfo:%@",remoteUserInfo);
//APP未启动,点击推送消息,iOS10下还是跟以前一样在此获取
}
}
}
-(NSString *)hexStringForData:(NSData *)data{
+(NSString *)hexStringForData:(NSData *)data{
if (data == nil) {
return nil;
......@@ -46,7 +52,7 @@
return hexString;
}
-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{
+(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{
OffcnIMPushModel *IMModel = [OffcnIMPushModel mj_ZYJobjectWithKeyValues:userInfo];
......@@ -62,4 +68,19 @@
[[OffcnIMZYJFMDBHandler sharedInstance] insertChatInfoToFMDBWithModel:body];
[[OffcnIMZYJFMDBHandler sharedInstance] chatlistCacheInfo:body callback:nil];
}
+(void)manualSendLocalNotificationWithModel:(OffcnIMModel *)model{
dispatch_sync(dispatch_get_main_queue(), ^{
UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init];
if (backgroudMsg) {
backgroudMsg.timeZone = [NSTimeZone defaultTimeZone];
backgroudMsg.alertBody = @"您收到一条新消息";
backgroudMsg.applicationIconBadgeNumber = 1;
backgroudMsg.alertAction = @"查看";
NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
backgroudMsg.userInfo = infoDic;
[[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];
}
});
}
@end
//
//
......@@ -18,6 +18,8 @@
@property (nonatomic, strong) OIMCoreAdapter *adapter;
@property (nonatomic, strong) NSMutableArray *msgArr;
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
@end
@implementation OffcnIMSocketManager
......@@ -43,6 +45,26 @@ static OffcnIMSocketManager *socketManager;
self.adapter = [OIMCoreAdapter sharedOIMCore];
[self.adapter Initialize];
self.adapter.delegate = self;
self.backgroundTask = UIBackgroundTaskInvalid;
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(appWillResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(appDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(appDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
......@@ -170,4 +192,39 @@ static OffcnIMSocketManager *socketManager;
}
}
}
- (void)appWillResignActive {
// [self.sessionManager disconnectWithDisconnectHandler:nil];
}
- (void)appDidEnterBackground {
// if (!self.sessionManager.requiresTearDown) {
// // we don't want to tear down session as it's already closed
// return;
// }
__weak typeof(self) weakSelf = self;
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf endBackgroundTask];
}];
}
- (void)appDidBecomeActive {
// [self.sessionManager connectToLast:nil];
}
- (void)endBackgroundTask {
if (self.backgroundTask) {
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}
}
- (void)dealloc {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[defaultCenter removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[defaultCenter removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
@end
......@@ -10,6 +10,7 @@
#import "OffcnIMSDKiOS.h"
#import "OffcnIMSocketManager.h"
#import "OffcnZYJTestDataStorageView.h"
#import "AppDelegate+OffcnIMPush.h"
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
......@@ -92,6 +93,8 @@
}else{
msg = [msg stringByAppendingString:IMModel.body.msg.url?IMModel.body.msg.url:@"nil"];
}
[AppDelegate manualSendLocalNotificationWithModel:IMModel];
// [weakSelf tipMessageWithTitle:@"收到消息" message:msg];
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment