// // ASPushOneSignalManager.m // Asteria // // Created by xingyu on 2024/7/11. // #import "ASPushOneSignalManager.h" #import @interface ASPushOneSignalManager() @end @implementation ASPushOneSignalManager + (instancetype)shared { static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } - (void)initOneSignalSDKWithPushConfig:(NSDictionary *)launchOptions { [OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_NONE]; // OneSignal initialization [OneSignal initWithLaunchOptions:launchOptions]; [OneSignal setAppId:@"618fe580-bc97-4bf4-b2bb-5039f9dbbc82"]; // promptForPushNotifications will show the native iOS notification permission prompt. // We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 8) [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) { NSLog(@"User accepted notifications: %d", accepted); }]; // [UNUserNotificationCenter currentNotificationCenter].delegate = self; // [self pushAuthRequest]; } - (void)handlePushAction:(NSDictionary *)payLoad { NSDictionary *customData = payLoad[@"custom"][@"a"]; NSString *title = customData[@"title"]; NSString *push_para = customData[@"push_para"]; NSString *message_id = customData[@"message_id"]; NSNumber *push_type = customData[@"push_type"]; if (!push_type) { return; } NSInteger type = push_type.integerValue; // [[KWPushMessageHandler shareInstance] handleMessage:title messageId:message_id pushPara:push_para pushType:type]; } #pragma mark ---- 设置用户推送标签 ---- - (void)setPushApnsTagWithUserInfo:(ASUserModel *)userInfo { if ([ASUserInfoManager shared].isLogin) { if (!userInfo) { userInfo = [[ASUserInfoManager shared] userInfo]; } if (!userInfo.Id.isEmpty) { NSInteger sufix = userInfo.Id.integerValue%10; [OneSignal sendTag:@"SufixUid" value:[NSString stringWithFormat:@"%ld", sufix]]; } if (userInfo.group_id.intValue == 5) { [OneSignal sendTag:@"IsOrdered" value:@"true"]; #if (DEBUG) [OneSignal sendTag:@"testOrderd" value:@"true"]; #endif } else { #if (DEBUG) [OneSignal deleteTag:@"testOrderd"]; #endif [OneSignal deleteTag:@"IsOrdered"]; } } else { [OneSignal sendTag:@"SufixUid" value:@"-"]; } } #pragma mark ---- UNUserNotificationCenterDelegate ---- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { // 用户点击了通知 // 通过response参数处理用户的点击事件 completionHandler(); NSDictionary *payLoad = response.notification.request.content.userInfo; [self handlePushAction:payLoad]; } - (void)pushAuthRequest { // 申请权限1 [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) { UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError *_Nullable error){ dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token }); }]; } }]; dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token }); } @end