| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // AppDelegate.m
- // Apex And Drivers
- //
- // Created by Ray on 2018/5/26.
- // Copyright © 2018 USAI. All rights reserved.
- //
- #import "AppDelegate.h"
- #import <UserNotifications/UserNotifications.h>
- #import "LoginViewController.h"
- #import "RAHomeViewController.h"
- @interface AppDelegate ()<UNUserNotificationCenterDelegate>
- @property (nonatomic,weak) UINavigationController *rootVC;
- @end
- @implementation AppDelegate
- #pragma mark - Private
- - (void)showHomeVC {
-
- RAHomeViewController *homeVC = [RAHomeViewController viewControllerFromStoryboard];
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeVC];
- self.window.rootViewController = nav;
- }
- - (void)saveUploadTasks {
-
- if(self.uploadManager.arr_queue!=nil)
- {
-
- [self.uploadManager stopAllTasks];
- [self.uploadManager saveTasks];
- }
- }
- #pragma mark - AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
-
- // View
-
- self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- self.window.backgroundColor = [UIColor whiteColor];
-
- LoginViewController *rootVC = [LoginViewController viewControllerFromStoryboard];
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
- self.window.rootViewController = nav;
-
- __weak typeof(self) weakSelf = self;
- rootVC.loginSuccessful = ^{
-
- [weakSelf showHomeVC];
- };
-
- [self.window makeKeyAndVisible];
-
- //消息推送注册
- UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
- [center setDelegate:self];
- UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
- [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
- if (granted) {
- NSLog(@"注册成功");
- }else{
- NSLog(@"注册失败");
- }
- }];
- [application registerForRemoteNotifications];
-
- return YES;
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
-
- if (!self.uploadManager) {
- self.uploadManager=[[RAUploadManager alloc] init];
- }
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- [self saveUploadTasks];
- }
- #pragma mark - Notification
- //完成注册后收到devicetoken
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- //上传deviceToken给后台服务器
- NSString *fullStr = [deviceToken description];
- NSLog(@"fullstr= %@",fullStr);
- NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
- NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSLog(@"注册推送失败:%@",error);
- }
- //处理通知
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
-
- UNNotificationRequest *request = notification.request;
- NSDictionary *userInfo = request.content.userInfo;
-
- if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
- NSLog(@"收到了一个远程推送:%@",userInfo);
- }
- else{
- NSLog(@"收到了一个本地推送:%@",userInfo);
- }
-
- completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
- }
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
- NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
-
- completionHandler();
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- NSLog(@"收到静默推送: %@",userInfo);
- completionHandler(UIBackgroundFetchResultNewData);
- }
- @end
|