AppDelegate+PushNotification.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // AppDelegate+PushNotification.m
  3. // Asteria
  4. //
  5. // Created by xingyu on 2024/7/16.
  6. //
  7. #import "AppDelegate+PushNotification.h"
  8. #import "ASJumpHandler.h"
  9. @implementation AppDelegate (PushNotification)
  10. - (void)initPushNotificationConfig:(NSDictionary *)launchOptions; {
  11. //初始化OneSignal SDK
  12. [ASOneSignalManager initOneSignalSDKWithPushConfig:launchOptions];
  13. //启动设置用户推送标签
  14. [ASOneSignalManager setPushApnsTagWithUserInfo];
  15. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  16. #ifdef DEBUG
  17. [PPNetworkHelper openLog];
  18. #else
  19. [PPNetworkHelper closeLog];
  20. #endif
  21. [self pushAuthRequest];
  22. }
  23. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  24. NSLog(@"--------userInfo:%@",userInfo);
  25. // completionHandler(UIBackgroundFetchResultNewData);
  26. [self handlePush:userInfo];
  27. }
  28. #pragma mark - dealwith push data
  29. /// 处理来自远程的推送内容
  30. - (void)handlePush:(NSDictionary *)payLoad {
  31. if (payLoad == nil) {
  32. return;
  33. }
  34. NSDictionary *aps = [payLoad valueForKey:@"aps"];
  35. if (aps == nil) {
  36. return;
  37. }
  38. UIApplication *application = [UIApplication sharedApplication];
  39. // 当前 APP 在前台
  40. if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) { //活动状态下使用消息提示再提示一下,让用户可以点击
  41. // 备注:这边比较特殊,当 APP 在前台时,当推送来的时候,会来到这个方法,当点击推送弹窗后,这个方法会再次调用,即这个方法会调用两次,走两次 push 操作.
  42. NSLog(@"payLoad=%@",payLoad);
  43. // [self handlePushAction:payLoad]; // 处理推送消息
  44. } else {
  45. [self handlePushAction:payLoad]; // 处理推送消息
  46. }
  47. }
  48. - (void)handlePushAction:(NSDictionary *)payLoad {
  49. NSDictionary *customData = payLoad[@"custom"][@"a"];
  50. NSString *title = customData[@"title"];
  51. NSString *push_para = customData[@"push_para"];
  52. NSString *message_id = customData[@"message_id"];
  53. NSNumber *push_type = customData[@"push_type"];
  54. if (!push_type) {
  55. return;
  56. }
  57. NSInteger type = push_type.integerValue;
  58. [[ASJumpHandler shareInstance] handleMessage:title messageId:message_id pushPara:push_para pushType:type];
  59. }
  60. #pragma mark ---- UNUserNotificationCenterDelegate ----
  61. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  62. // 用户点击了通知
  63. // 通过response参数处理用户的点击事件
  64. completionHandler();
  65. NSDictionary *payLoad = response.notification.request.content.userInfo;
  66. [self handlePushAction:payLoad];
  67. }
  68. - (void)pushAuthRequest {
  69. // 申请权限1
  70. [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  71. if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
  72. UNAuthorizationOptions authOptions =
  73. UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  74. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions
  75. completionHandler:^(BOOL granted, NSError *_Nullable error){
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token
  78. });
  79. }];
  80. }
  81. }];
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token
  84. });
  85. }
  86. - (void)authPush {
  87. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  88. UNAuthorizationOptions authOptions =
  89. UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  90. __weak typeof(self) weakSelf = self;
  91. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions
  92. completionHandler:^(BOOL granted, NSError *_Nullable error){
  93. dispatch_async(dispatch_get_main_queue(), ^{
  94. [weakSelf pushAuthRequest];
  95. [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token
  96. });
  97. }];
  98. }
  99. - (void)clearBadge {
  100. [UIApplication sharedApplication].applicationIconBadgeNumber = -1;
  101. }
  102. -(void)oneSignalPushConfig:(NSDictionary *)launchOptions{
  103. /*
  104. //#ifdef DEBUG
  105. // [MobPush setAPNsForProduction:NO];
  106. //#else
  107. // [MobPush setAPNsForProduction:YES];
  108. //#endif
  109. // //设置地区:regionId 默认0(国内),1:海外
  110. // [MobPush setRegionID:1];
  111. // //MobPush推送设置(获得角标、声音、弹框提醒权限)
  112. // MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];
  113. // configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;
  114. // [MobPush setupNotification:configuration];
  115. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];
  116. */
  117. // // Remove this method to stop OneSignal Debugging
  118. // [OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_NONE];
  119. //
  120. // // OneSignal initialization
  121. // [OneSignal initWithLaunchOptions:launchOptions];
  122. // [OneSignal setAppId:@"618fe580-bc97-4bf4-b2bb-5039f9dbbc82"];
  123. //
  124. // // promptForPushNotifications will show the native iOS notification permission prompt.
  125. // // We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 8)
  126. // [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
  127. // NSLog(@"User accepted notifications: %d", accepted);
  128. // }];
  129. // Set your customer userId
  130. // [OneSignal setExternalUserId:@"userId"];
  131. // Pass in email provided by customer
  132. // [OneSignal setEmail:@"example@domain.com"];
  133. // Pass in phone number provided by customer
  134. // [OneSignal setSMSNumber:@"+11234567890"];
  135. // [OneSignal sendTag:@"key" value:@"value"];
  136. #ifdef DEBUG
  137. [PPNetworkHelper openLog];
  138. #else
  139. [PPNetworkHelper closeLog];
  140. #endif
  141. }
  142. @end