AppDelegate.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // AppDelegate.m
  3. // Apex And Drivers
  4. //
  5. // Created by Ray on 2018/5/26.
  6. // Copyright © 2018 USAI. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import <UserNotifications/UserNotifications.h>
  10. #import "LoginViewController.h"
  11. #import "RAHomeViewController.h"
  12. @interface AppDelegate ()<UNUserNotificationCenterDelegate>
  13. @property (nonatomic,weak) UINavigationController *rootVC;
  14. @end
  15. @implementation AppDelegate
  16. #pragma mark - Private
  17. - (void)showHomeVC {
  18. RAHomeViewController *homeVC = [RAHomeViewController viewControllerFromStoryboard];
  19. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeVC];
  20. self.window.rootViewController = nav;
  21. }
  22. - (void)saveUploadTasks {
  23. if(self.uploadManager.arr_queue!=nil)
  24. {
  25. [self.uploadManager stopAllTasks];
  26. [self.uploadManager saveTasks];
  27. }
  28. }
  29. #pragma mark - AppDelegate
  30. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  31. // Override point for customization after application launch.
  32. // View
  33. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  34. self.window.backgroundColor = [UIColor whiteColor];
  35. LoginViewController *rootVC = [LoginViewController viewControllerFromStoryboard];
  36. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
  37. self.window.rootViewController = nav;
  38. __weak typeof(self) weakSelf = self;
  39. rootVC.loginSuccessful = ^{
  40. [weakSelf showHomeVC];
  41. };
  42. [self.window makeKeyAndVisible];
  43. //消息推送注册
  44. UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  45. [center setDelegate:self];
  46. UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
  47. [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
  48. if (granted) {
  49. NSLog(@"注册成功");
  50. }else{
  51. NSLog(@"注册失败");
  52. }
  53. }];
  54. [application registerForRemoteNotifications];
  55. return YES;
  56. }
  57. - (void)applicationDidBecomeActive:(UIApplication *)application {
  58. if (!self.uploadManager) {
  59. self.uploadManager=[[RAUploadManager alloc] init];
  60. }
  61. }
  62. - (void)applicationWillTerminate:(UIApplication *)application {
  63. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  64. [self saveUploadTasks];
  65. }
  66. #pragma mark - Notification
  67. //完成注册后收到devicetoken
  68. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  69. //上传deviceToken给后台服务器
  70. NSString *fullStr = [deviceToken description];
  71. NSLog(@"fullstr= %@",fullStr);
  72. NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
  73. NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
  74. }
  75. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  76. NSLog(@"注册推送失败:%@",error);
  77. }
  78. //处理通知
  79. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  80. UNNotificationRequest *request = notification.request;
  81. NSDictionary *userInfo = request.content.userInfo;
  82. if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  83. NSLog(@"收到了一个远程推送:%@",userInfo);
  84. }
  85. else{
  86. NSLog(@"收到了一个本地推送:%@",userInfo);
  87. }
  88. completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
  89. }
  90. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  91. NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
  92. completionHandler();
  93. }
  94. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  95. NSLog(@"收到静默推送: %@",userInfo);
  96. completionHandler(UIBackgroundFetchResultNewData);
  97. }
  98. @end