AppDelegate.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // AppDelegate.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/21.
  6. //
  7. #import "AppDelegate.h"
  8. #import "RDSRootControl.h"
  9. #import <MMKV/MMKV.h>
  10. #import <WebKit/WebKit.h>
  11. #import <GTSDK/GeTuiSdk.h>
  12. #import "Utils.h"
  13. @interface AppDelegate ()<GeTuiSdkDelegate>
  14. @property (nonatomic, strong) WKWebView *webView;
  15. @end
  16. @implementation AppDelegate
  17. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  18. // Override point for customization after application launch.
  19. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  20. [RDSRootControl shareControl].isFirst = YES;
  21. self.window.rootViewController = [RDSRootControl shareControl].rootViewController;
  22. [self.window makeKeyAndVisible];
  23. self.window.backgroundColor = RDSViewBgColor;
  24. // 初始化MMKV
  25. // [MMKV initializeMMKV:nil];
  26. // 设置全局userAgent
  27. self.webView = [[WKWebView alloc] init];
  28. [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  29. NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
  30. [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
  31. }];
  32. // 自动登录
  33. if(!kNULLString(TheDataManager.token)){
  34. [RDSDemoApiHelper rds_refreshTokenSuccess:^(id responseObject) {
  35. if ([responseObject[@"code"] intValue] == 0) {
  36. [RDSRootControl shareControl].isLoginSuccess = YES;
  37. NSDictionary *data = responseObject[@"data"];
  38. TheDataManager.token = [NSString stringWithFormat:@"%@ %@",data[@"token_type"], data[@"access_token"]];
  39. } else{
  40. }
  41. } failure:^(NSError *error) {
  42. }];
  43. }
  44. self.window.userInteractionEnabled = YES;
  45. // [RDSHudShower setupHUD];
  46. // 启动图片延时1秒
  47. // [NSThread sleepForTimeInterval:1];
  48. NSDictionary *dic = [[NSDictionary alloc]init];
  49. [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self launchingOptions:dic];
  50. UNAuthorizationOptions types = (UNAuthorizationOptionBadge |
  51. UNAuthorizationOptionAlert |
  52. UNAuthorizationOptionSound);
  53. [GeTuiSdk registerRemoteNotification:types];
  54. //清空通知和角标
  55. [GeTuiSdk clearAllNotificationForNotificationBar];
  56. [self p_appInit];
  57. // NSString *language = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
  58. // DDLog(@"系统语言:%@",language);
  59. // [[NSUserDefaults standardUserDefaults] setObject:@[@"zh-Hans"] forKey:@"AppleLanguages"];
  60. // [[NSUserDefaults standardUserDefaults] synchronize];
  61. return YES;
  62. }
  63. -(void)p_appInit
  64. {
  65. [RDSDemoApiHelper rds_appInitWithType:@1 success:^(id responseObject) {
  66. } failure:^(NSError *error) {
  67. }];
  68. }
  69. /// [ GTSDK回调 ] SDK启动成功返回cid
  70. - (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
  71. NSLog(@"----clientId----%@", clientId);
  72. [[NSUserDefaults standardUserDefaults] setObject:clientId forKey:@"cid"];
  73. }
  74. - (void)GeTuiSdkNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification completionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  75. // NSString *msg = [NSString stringWithFormat:@"[ TestDemo ] [APNs] %@ \n%@", NSStringFromSelector(_cmd), notification.request.content.userInfo];
  76. // [ 参考代码,开发者注意根据实际需求自行修改 ] 根据APP需要,判断是否要提示用户Badge、Sound、Alert等
  77. //completionHandler(UNNotificationPresentationOptionNone); 若不显示通知,则无法点击通知
  78. completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner);
  79. }
  80. - (void)GeTuiSdkDidReceiveNotification:(NSDictionary *)userInfo notificationCenter:(UNUserNotificationCenter *)center response:(UNNotificationResponse *)response fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  81. if(completionHandler) {
  82. // [ 参考代码,开发者注意根据实际需求自行修改 ] 根据APP需要自行修改参数值
  83. completionHandler(UIBackgroundFetchResultNoData);
  84. }
  85. }
  86. - (void)GeTuiSdkDidReceiveSlience:(NSDictionary *)userInfo fromGetui:(BOOL)fromGetui offLine:(BOOL)offLine appId:(NSString *)appId taskId:(NSString *)taskId msgId:(NSString *)msgId fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  87. //本地通知UserInfo参数
  88. NSDictionary *dic = nil;
  89. if (fromGetui) {
  90. //个推在线透传
  91. //个推进行本地通知统计 userInfo中必须要有_gmid_参数
  92. dic = @{@"_gmid_": [NSString stringWithFormat:@"%@:%@", taskId ?: @"", msgId ?: @""]};
  93. } else {
  94. //APNs静默通知
  95. dic = userInfo;
  96. }
  97. if (fromGetui && offLine == NO) {
  98. //个推通道+在线,发起本地通知
  99. [Utils pushLocalNotification:userInfo[@"payload"] userInfo:dic];
  100. }
  101. if(completionHandler) {
  102. // [ 参考代码,开发者注意根据实际需求自行修改 ] 根据APP需要自行修改参数值
  103. completionHandler(UIBackgroundFetchResultNoData);
  104. }
  105. }
  106. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
  107. {
  108. //注意,如果执行了上述代理方法中的completionHandler,就不要执行当前回调中的completionHandler。
  109. //防止两次调用completionHandler引起崩溃
  110. //completionHandler(UIBackgroundFetchResultNewData);
  111. }
  112. -(void)applicationDidEnterBackground:(UIApplication *)application
  113. {
  114. [kNotificationCenter postNotificationName:@"kApplicationDidEnterBackground" object:nil];
  115. }
  116. -(void)applicationWillEnterForeground:(UIApplication *)application
  117. {
  118. [kNotificationCenter postNotificationName:@"kApplicationWillEnterForeground" object:nil];
  119. }
  120. @end