// // AppDelegate.m // ZBProject // // Created by 学丽 on 2019/3/26. // Copyright © 2019 ZB. All rights reserved. // #import "AppDelegate.h" #import "ZBTabController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self initWindows]; [self getUserInfo]; [self setUM]; [self setupMiPush]; [self getRemoteNotificationDictWithDict:launchOptions]; [self cancelIconBadgeNumber]; return YES; } #pragma mark---获取用户信息 -(void)getUserInfo { if ([AccountTool isLogin]) { [ZBHTTP post:getUserInfoURL params:nil success:^(id _Nonnull json) { NSDictionary *dics =(NSDictionary *)json[@"data"]; AccountModel *oldM =[AccountTool account]; oldM.name = dics[@"name"]; oldM.img=dics[@"img"]; [AccountTool saveAccount:oldM]; } failure:^(NSError * _Nonnull error) { }]; } } - (void)getRemoteNotificationDictWithDict:(NSDictionary *)tfdic { NSDictionary *remoteNotification = [tfdic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if(remoteNotification){ NSString *messageId; if ([remoteNotification containsObjectForKey:@"_id_"]) { messageId = [remoteNotification objectForKey:@"_id_"]; } if (messageId!=nil) { [MiPushSDK openAppNotify:messageId]; } [self jumpViewController:remoteNotification]; } } #pragma mark---小米推送 - (void)setupMiPush { [MiPushSDK registerMiPush:self]; } #pragma mark--友盟统计 -(void)setUM { UMConfigInstance.appKey=UMENG_KEY; UMConfigInstance.channelId=@"App Store"; NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"]; [MobClick setAppVersion:appVersion]; [MobClick startWithConfigure:UMConfigInstance]; [MobClick setLogEnabled:YES]; } #pragma mark---初始化 -(void)initWindows { self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; ZBTabController *tabv= [[ZBTabController alloc]init]; ZBSliderViewController *sliv=[[ZBSliderViewController alloc]init]; WBSliderViewController *slider=[[WBSliderViewController alloc]initWithMainViewController:tabv leftViewController:sliv rightViewController:nil]; self.window.rootViewController=slider; [self.window makeKeyAndVisible]; AccountModel *userModel =[AccountTool account]; if (![AccountTool isLogin] || userModel.has_kwai.integerValue !=1 ) { [self.window.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[ZBLoginTipeViewController alloc]init]] animated:YES completion:nil]; } } #pragma mark 注册push服务. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"APNS token: %@", [deviceToken description]); // 注册APNS成功, 注册deviceToken [MiPushSDK bindDeviceToken:deviceToken]; } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSLog(@"APNS error: %@", err); // 注册APNS失败. // 自行处理. } #pragma mark MiPushSDKDelegate - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data { // 请求成功 // 可在此获取regId if ([selector isEqualToString:@"bindDeviceToken:"]) { NSLog(@"regid = %@", data[@"regid"]); } } - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data { // 请求失败 } #pragma mark Local And Push Notification - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"APNS notify: %@", userInfo); // 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回 [MiPushSDK handleReceiveRemoteNotification:userInfo]; } // iOS10新加入的回调方法 // 应用在前台收到通知 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) { NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"APNS notify: %@", userInfo);; [MiPushSDK handleReceiveRemoteNotification:userInfo]; NSString *messageId = [userInfo objectForKey:@"_id_"]; [MiPushSDK openAppNotify:messageId]; } [self cancelIconBadgeNumber]; } - (void)cancelIconBadgeNumber { // 注册显示应用程序BadgeNumber的通知 UIApplication *application = [UIApplication sharedApplication]; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [application registerUserNotificationSettings:settings]; if (application.applicationIconBadgeNumber > 0) { application.applicationIconBadgeNumber = 0; } } // 点击通知进入应用 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) { NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"APNS notify: %@", userInfo); [MiPushSDK handleReceiveRemoteNotification:userInfo]; NSString *messageId = [userInfo objectForKey:@"_id_"]; [MiPushSDK openAppNotify:messageId]; } [self jumpViewController:userInfo]; completionHandler(); } - (void)jumpViewController:(NSDictionary *)remoteNotification { [self cancelIconBadgeNumber]; NSInteger pushType = [remoteNotification[@"pushType"] integerValue]; switch (pushType) { case 1005://跳转到消息列表 [self pushMessage]; break; default: break; } } #pragma mark---消息列表 -(void)pushMessage { ZBMessageViewController *message=[[ZBMessageViewController alloc]init]; [[self currentViewController]presentViewController:[[UINavigationController alloc]initWithRootViewController:message] animated:NO completion:nil]; } - (UIViewController *)currentViewController{ UIWindow* window = [[[UIApplication sharedApplication] delegate] window]; NSAssert(window, @"The window is empty"); return window.rootViewController; } - (void)applicationWillResignActive:(UIApplication *)application { // 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 invalidate graphics rendering callbacks. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end