AppDelegate+PushNotification.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. [self pushAuthRequest];
  17. #ifdef DEBUG
  18. [PPNetworkHelper openLog];
  19. #else
  20. [PPNetworkHelper closeLog];
  21. #endif
  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)clearBadge {
  87. [UIApplication sharedApplication].applicationIconBadgeNumber = -1;
  88. }
  89. -(void)oneSignalPushConfig:(NSDictionary *)launchOptions{
  90. /*
  91. //#ifdef DEBUG
  92. // [MobPush setAPNsForProduction:NO];
  93. //#else
  94. // [MobPush setAPNsForProduction:YES];
  95. //#endif
  96. // //设置地区:regionId 默认0(国内),1:海外
  97. // [MobPush setRegionID:1];
  98. // //MobPush推送设置(获得角标、声音、弹框提醒权限)
  99. // MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];
  100. // configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;
  101. // [MobPush setupNotification:configuration];
  102. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];
  103. */
  104. // // Remove this method to stop OneSignal Debugging
  105. // [OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_NONE];
  106. //
  107. // // OneSignal initialization
  108. // [OneSignal initWithLaunchOptions:launchOptions];
  109. // [OneSignal setAppId:@"618fe580-bc97-4bf4-b2bb-5039f9dbbc82"];
  110. //
  111. // // promptForPushNotifications will show the native iOS notification permission prompt.
  112. // // We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 8)
  113. // [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
  114. // NSLog(@"User accepted notifications: %d", accepted);
  115. // }];
  116. // Set your customer userId
  117. // [OneSignal setExternalUserId:@"userId"];
  118. // Pass in email provided by customer
  119. // [OneSignal setEmail:@"example@domain.com"];
  120. // Pass in phone number provided by customer
  121. // [OneSignal setSMSNumber:@"+11234567890"];
  122. // [OneSignal sendTag:@"key" value:@"value"];
  123. #ifdef DEBUG
  124. [PPNetworkHelper openLog];
  125. #else
  126. [PPNetworkHelper closeLog];
  127. #endif
  128. }
  129. @end