// // AppDelegate+PushNotification.m // Asteria // // Created by xingyu on 2024/7/16. // #import "AppDelegate+PushNotification.h" #import "ASJumpHandler.h" @implementation AppDelegate (PushNotification) - (void)initPushNotificationConfig:(NSDictionary *)launchOptions; { //初始化OneSignal SDK [ASOneSignalManager initOneSignalSDKWithPushConfig:launchOptions]; //启动设置用户推送标签 [ASOneSignalManager setPushApnsTagWithUserInfo]; [UNUserNotificationCenter currentNotificationCenter].delegate = self; #ifdef DEBUG [PPNetworkHelper openLog]; #else [PPNetworkHelper closeLog]; #endif [self pushAuthRequest]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"--------userInfo:%@",userInfo); // completionHandler(UIBackgroundFetchResultNewData); [self handlePush:userInfo]; } #pragma mark - dealwith push data /// 处理来自远程的推送内容 - (void)handlePush:(NSDictionary *)payLoad { if (payLoad == nil) { return; } NSDictionary *aps = [payLoad valueForKey:@"aps"]; if (aps == nil) { return; } UIApplication *application = [UIApplication sharedApplication]; // 当前 APP 在前台 if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) { //活动状态下使用消息提示再提示一下,让用户可以点击 // 备注:这边比较特殊,当 APP 在前台时,当推送来的时候,会来到这个方法,当点击推送弹窗后,这个方法会再次调用,即这个方法会调用两次,走两次 push 操作. NSLog(@"payLoad=%@",payLoad); // [self handlePushAction:payLoad]; // 处理推送消息 } else { [self handlePushAction:payLoad]; // 处理推送消息 } } - (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; [[ASJumpHandler shareInstance] handleMessage:title messageId:message_id pushPara:push_para pushType:type]; } #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 }); } - (void)authPush { [UNUserNotificationCenter currentNotificationCenter].delegate = self; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; __weak typeof(self) weakSelf = self; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError *_Nullable error){ dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf pushAuthRequest]; [[UIApplication sharedApplication] registerForRemoteNotifications]; //注册获得device Token }); }]; } - (void)clearBadge { [UIApplication sharedApplication].applicationIconBadgeNumber = -1; } -(void)oneSignalPushConfig:(NSDictionary *)launchOptions{ /* //#ifdef DEBUG // [MobPush setAPNsForProduction:NO]; //#else // [MobPush setAPNsForProduction:YES]; //#endif // //设置地区:regionId 默认0(国内),1:海外 // [MobPush setRegionID:1]; // //MobPush推送设置(获得角标、声音、弹框提醒权限) // MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init]; // configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert; // [MobPush setupNotification:configuration]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil]; */ // // Remove this method to stop OneSignal Debugging // [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); // }]; // Set your customer userId // [OneSignal setExternalUserId:@"userId"]; // Pass in email provided by customer // [OneSignal setEmail:@"example@domain.com"]; // Pass in phone number provided by customer // [OneSignal setSMSNumber:@"+11234567890"]; // [OneSignal sendTag:@"key" value:@"value"]; #ifdef DEBUG [PPNetworkHelper openLog]; #else [PPNetworkHelper closeLog]; #endif } @end