浏览代码

ios 增加本地通知点击监听

zhangzy 7 年之前
父节点
当前提交
9e7cfd963c
共有 4 个文件被更改,包括 59 次插入12 次删除
  1. 22 2
      index.js
  2. 2 2
      ios/RCTMIPushModule/RCTMIPushModule/RCTMIPushModule.h
  3. 34 7
      ios/RCTMIPushModule/RCTMIPushModule/RCTMIPushModule.m
  4. 1 1
      package.json

+ 22 - 2
index.js

@@ -97,7 +97,17 @@ class MIPush extends NativeEventEmitter {
 
         if(Platform.OS == 'ios') {
 
-            PushNotificationIOS.addEventListener(type, handler);
+            switch (type) {
+                case "notification":
+                case "localNotification":
+                case "register":
+                    PushNotificationIOS.addEventListener(type, handler);
+                    break;
+                default:
+                    this.addListener(type, handler);
+                    break;
+            }
+
         } else {
 
             this.addListener(type, handler);
@@ -108,7 +118,17 @@ class MIPush extends NativeEventEmitter {
 
         if(Platform.OS == 'ios') {
 
-            PushNotificationIOS.removeEventListener(type);
+            switch (type) {
+                case "notification":
+                case "localNotification":
+                case "register":
+                    PushNotificationIOS.removeEventListener(type);
+                    break;
+                default:
+                    this.removeListener(type);
+                    break;
+            }
+
         } else {
 
             this.removeListener(type);

+ 2 - 2
ios/RCTMIPushModule/RCTMIPushModule/RCTMIPushModule.h

@@ -9,11 +9,11 @@
 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
 #import <UserNotifications/UserNotifications.h>
-#import <React/RCTBridgeModule.h>
+#import <React/RCTEventEmitter.h>
 #import "RCTPushNotificationManager.h"
 #import "MiPushSDK.h"
 
-@interface RCTMIPushModule : NSObject <RCTBridgeModule, MiPushSDKDelegate>
+@interface RCTMIPushModule : RCTEventEmitter <RCTBridgeModule, MiPushSDKDelegate>
 
 + (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
 

+ 34 - 7
ios/RCTMIPushModule/RCTMIPushModule/RCTMIPushModule.m

@@ -78,39 +78,66 @@ RCT_EXPORT_METHOD(unsetAccount:(NSString *)text)
 
 + (void)application:(id)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
     
-    NSLog(@"deviceToken:%@", deviceToken);
     [MiPushSDK bindDeviceToken:deviceToken];
     [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
 }
 
 + (void)application:(id)application didReceiveRemoteNotification:(NSDictionary *)notification {
-    
+
     [RCTPushNotificationManager didReceiveRemoteNotification:notification];
 }
 
 + (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
-    
+
     [RCTPushNotificationManager didReceiveLocalNotification:notification];
+    [[NSNotificationCenter defaultCenter] postNotificationName:@"xmpush_click" object:notification.userInfo];
 }
 
 // 应用在前台收到通知
 + (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
-    
+
     NSDictionary * userInfo = notification.request.content.userInfo;
     if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
-        
+
         [RCTPushNotificationManager didReceiveRemoteNotification:userInfo];
     }
 }
 
 // 点击通知进入应用
 + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
-    
     NSDictionary * userInfo = response.notification.request.content.userInfo;
     if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
-        
         [RCTPushNotificationManager didReceiveRemoteNotification:userInfo];
     }
+    [[NSNotificationCenter defaultCenter] postNotificationName:@"xmpush_click" object:userInfo];
+    completionHandler();
+}
+
+- (void) handleSend:(NSNotification *)notification {
+
+    [self sendEventWithName:notification.name body:notification.object];
+}
+
+- (void)startObserving
+{
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(handleSend:)
+                                                 name:@"xmpush_click"
+                                               object:nil];
+
+}
+
+- (void)stopObserving
+{
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+- (NSArray<NSString *> *)supportedEvents {
+    NSMutableArray *arr = [[NSMutableArray alloc] init];
+
+    [arr addObject:@"xmpush_click"];
+
+    return arr;
 }
 
 @end

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "react-native-xmpush",
-  "version": "0.2.2",
+  "version": "0.2.3",
   "description": "集成小米推送",
   "main": "index.js",
   "scripts": {