leon 1 anno fa
parent
commit
5237f4a933
3 ha cambiato i file con 45 aggiunte e 22 eliminazioni
  1. 2 2
      ios/AppDelegate.h
  2. 42 19
      ios/AppDelegate.mm
  3. 1 1
      ios/NativeBridge.m

+ 2 - 2
ios/AppDelegate.h

@@ -1,10 +1,10 @@
 #import <React/RCTBridgeDelegate.h>
-#import <JPUSHService.h>
+//#import <JPUSHService.h>
 #import <Expo/Expo.h>
 #import <UIKit/UIKit.h>
 #import "NativeBridge.h"
 
-@interface AppDelegate : EXAppDelegateWrapper <UIApplicationDelegate, RCTBridgeDelegate,JPUSHRegisterDelegate>
+@interface AppDelegate : EXAppDelegateWrapper <UIApplicationDelegate, RCTBridgeDelegate,UNUserNotificationCenterDelegate>
 
 @property (nonatomic, strong) UIWindow *window;
 @property (nonatomic, strong) NativeBridge *nativeBridge;

+ 42 - 19
ios/AppDelegate.mm

@@ -5,7 +5,7 @@
 #import <React/RCTRootView.h>
 
 #import <React/RCTAppSetupUtils.h>
-#import <RCTJPushModule.h>
+//#import <RCTJPushModule.h>
 
 #if RCT_NEW_ARCH_ENABLED
 #import <React/CoreModulesPlugins.h>
@@ -45,15 +45,18 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
   //加上时差,得到本地时间
   NSDate *localeDate = [date dateByAddingTimeInterval: interval];
 
-    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
-    if (@available(iOS 12.0, *)) {
-      entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
-    }
-    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
+//    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
+//    if (@available(iOS 12.0, *)) {
+//      entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
+//    }
+//    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
   
   RCTAppSetupPrepareApp(application);
 
   RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
+  
+  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+  center.delegate = self;
 
 #if RCT_NEW_ARCH_ENABLED
   _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
@@ -194,7 +197,7 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
 
 //注册 APNS 成功并上报 DeviceToken
 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
-  [JPUSHService registerDeviceToken:deviceToken];
+//  [JPUSHService registerDeviceToken:deviceToken];
 }
 
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
@@ -205,8 +208,8 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:  (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
   // iOS 10 以下 Required
   NSLog(@"iOS 7 APNS");
-  [JPUSHService handleRemoteNotification:userInfo];
-  [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
+//  [JPUSHService handleRemoteNotification:userInfo];
+//  [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
   completionHandler(UIBackgroundFetchResultNewData);
 }
 
@@ -216,13 +219,13 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
   if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
     // Apns
     NSLog(@"iOS 10 APNS 前台收到消息");
-    [JPUSHService handleRemoteNotification:userInfo];
-    [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
+//    [JPUSHService handleRemoteNotification:userInfo];
+//    [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
   }
   else {
     // 本地通知 todo
     NSLog(@"iOS 10 本地通知 前台收到消息");
-    [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
+//    [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
   }
   //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
   completionHandler(UNNotificationPresentationOptionAlert);
@@ -232,23 +235,43 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
   
 }
 
+- (void)application:(UIApplication *)application didReceiveLocalNotification:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
+    if ([response.actionIdentifier isEqualToString:@"action1"]) {
+        // 处理"动作1"
+    } else if ([response.actionIdentifier isEqualToString:@"action2"]) {
+        // 处理"动作2"
+    }
+    completionHandler();
+}
+
+//再实现UNUserNotificationCenterDelegate代理的方法
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
+{
+    //应用在前台时候接收到本地推送通知、远程推送通知调用此方法
+}
+
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
+{
+    //应用程序在后台,用户通过点击本地推送、远程推送进入app时调用此方法
+}
+
 //iOS 10 消息事件回调
 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
   NSDictionary * userInfo = response.notification.request.content.userInfo;
   if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
     // Apns
     NSLog(@"iOS 10 APNS 消息事件回调");
-    [JPUSHService handleRemoteNotification:userInfo];
-    // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
-    [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
-    [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
+//    [JPUSHService handleRemoteNotification:userInfo];
+//    // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
+//    [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
+//    [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
   }
   else {
     // 本地通知
     NSLog(@"iOS 10 本地通知 消息事件回调");
     // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
-    [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
-    [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
+//    [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
+//    [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
   }
   self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(postNotificationData:) userInfo:response repeats:YES];
 //  NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
@@ -334,7 +357,7 @@ static NSString *const kRNConcurrentRoot = @"concurrentRoot";
 //自定义消息
 - (void)networkDidReceiveMessage:(NSNotification *)notification {
   NSDictionary * userInfo = [notification userInfo];
-  [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
+//  [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
 }
 
 - (void)demo{

+ 1 - 1
ios/NativeBridge.m

@@ -231,7 +231,7 @@ RCT_EXPORT_METHOD(addLocalPush:(id)array){
         // 计算当前时间与给定时间戳之间的时间差(秒)
         NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
         
-        UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
+        UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
         NSString * identifier = message_id;
         UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
         [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {