ASPushOneSignalManager.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // ASPushOneSignalManager.m
  3. // Asteria
  4. //
  5. // Created by xingyu on 2024/7/11.
  6. //
  7. #import "ASPushOneSignalManager.h"
  8. #import <OneSignal/OneSignal.h>
  9. @interface ASPushOneSignalManager()<UNUserNotificationCenterDelegate>
  10. @end
  11. @implementation ASPushOneSignalManager
  12. + (instancetype)shared {
  13. static id sharedInstance = nil;
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. sharedInstance = [[self alloc] init];
  17. });
  18. return sharedInstance;
  19. }
  20. - (void)initOneSignalSDKWithPushConfig:(NSDictionary *)launchOptions {
  21. [OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_NONE];
  22. // OneSignal initialization
  23. [OneSignal initWithLaunchOptions:launchOptions];
  24. [OneSignal setAppId:@"618fe580-bc97-4bf4-b2bb-5039f9dbbc82"];
  25. // promptForPushNotifications will show the native iOS notification permission prompt.
  26. // We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 8)
  27. [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
  28. NSLog(@"User accepted notifications: %d", accepted);
  29. }];
  30. // [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  31. // [self pushAuthRequest];
  32. }
  33. - (void)handlePushAction:(NSDictionary *)payLoad {
  34. NSDictionary *customData = payLoad[@"custom"][@"a"];
  35. NSString *title = customData[@"title"];
  36. NSString *push_para = customData[@"push_para"];
  37. NSString *message_id = customData[@"message_id"];
  38. NSNumber *push_type = customData[@"push_type"];
  39. if (!push_type) {
  40. return;
  41. }
  42. NSInteger type = push_type.integerValue;
  43. // [[KWPushMessageHandler shareInstance] handleMessage:title messageId:message_id pushPara:push_para pushType:type];
  44. }
  45. #pragma mark ---- 设置用户推送标签 ----
  46. - (void)setPushApnsTagWithUserInfo:(ASUserModel *)userInfo {
  47. if ([ASUserInfoManager shared].isLogin) {
  48. if (!userInfo) {
  49. userInfo = [[ASUserInfoManager shared] userInfo];
  50. }
  51. if (!userInfo.Id.isEmpty) {
  52. NSInteger sufix = userInfo.Id.integerValue%10;
  53. [OneSignal sendTag:@"SufixUid" value:[NSString stringWithFormat:@"%ld", sufix]];
  54. }
  55. if (userInfo.group_id.intValue == 5) {
  56. [OneSignal sendTag:@"IsOrdered" value:@"true"];
  57. #if (DEBUG)
  58. [OneSignal sendTag:@"testOrderd" value:@"true"];
  59. #endif
  60. } else {
  61. #if (DEBUG)
  62. [OneSignal deleteTag:@"testOrderd"];
  63. #endif
  64. [OneSignal deleteTag:@"IsOrdered"];
  65. }
  66. } else {
  67. [OneSignal sendTag:@"SufixUid" value:@"-"];
  68. }
  69. }
  70. #pragma mark ---- UNUserNotificationCenterDelegate ----
  71. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  72. // 用户点击了通知
  73. // 通过response参数处理用户的点击事件
  74. completionHandler();
  75. NSDictionary *payLoad = response.notification.request.content.userInfo;
  76. [self handlePushAction:payLoad];
  77. }
  78. - (void)pushAuthRequest {
  79. // 申请权限1
  80. [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  81. if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
  82. UNAuthorizationOptions authOptions =
  83. UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  84. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions
  85. completionHandler:^(BOOL granted, NSError *_Nullable error){
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token
  88. });
  89. }];
  90. }
  91. }];
  92. dispatch_async(dispatch_get_main_queue(), ^{
  93. [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token
  94. });
  95. }
  96. @end