//
//  AppDelegate+OffcnIMPush.m
//  OffcnIMSDKiOS
//
//  Created by vockey on 2020/9/1.
//  Copyright © 2020 offcn. All rights reserved.
//

#import "AppDelegate+OffcnIMPush.h"
#import "OffcnIMModel.h"
#import "ZYJMJExtension.h"
#import "DataSafe.h"
#import "OffcnIMZYJFMDBHandler.h"

@implementation AppDelegate (OffcnIMPush)

-(void)registerOffcnUserNotificationSettingsWithApplication:(UIApplication *)application options:(NSDictionary *)launchOptions{
    //    application.applicationIconBadgeNumber = 0;
    
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
        // 1.请求授权可以给用户发送通知
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil];
        
        [application registerUserNotificationSettings:settings];
    }else{
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
        // 跳转
    }
}

-(NSString *)hexStringForData:(NSData *)data{

    if (data == nil) {
        return nil;
    }

    NSMutableString *hexString = [NSMutableString string];
    const unsigned char *p = [data bytes];
    for (int i=0; i < [data length]; i++) {
        [hexString appendFormat:@"%02x", *p++];
    }
    return hexString;
}

-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
    NSString *revStr = @"";
    OffcnIMModel *IMModel = [OffcnIMModel mj_ZYJobjectWithKeyValues:revStr];
    
    //存数据库
    OffcnIMBodyModel *body = IMModel.body;
    body.isSentSuccess = 1;
    body.bymyself = 0;
    body.isRead = 0;
    [[OffcnIMZYJFMDBHandler sharedInstance] insertChatInfoToFMDBWithModel:body];
    [[OffcnIMZYJFMDBHandler sharedInstance] chatlistCacheInfo:body callback:nil];
}
@end
