From c6f86241d3d66eeeb55ce5dd0d4dd5baa4cd0ef7 Mon Sep 17 00:00:00 2001 From: Jeepeng Date: Fri, 9 Jun 2017 16:21:35 +0800 Subject: [PATCH] =?UTF-8?q?app=E8=BF=90=E8=A1=8C=E6=97=B6=EF=BC=8Cnotifica?= =?UTF-8?q?tion=20=E5=8A=A0=E5=85=A5=20clicked=20=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E6=A0=87=E8=AE=B0=E9=80=9A=E7=9F=A5=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=A2=AB=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeepeng/react/xgpush/PushModule.java | 15 ++++++----- .../xgpush/receiver/MessageReceiver.java | 26 ++++++++++++------- example/app/index.js | 6 ++++- example/ios/example/AppDelegate.m | 23 +++++++--------- index.js | 2 +- package.json | 2 +- 6 files changed, 43 insertions(+), 31 deletions(-) diff --git a/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java b/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java index 9d8603f..0006df2 100644 --- a/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java +++ b/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java @@ -74,7 +74,7 @@ public void onReceive(Context context, Intent intent) { case Constants.ACTION_ON_TEXT_MESSAGE: String title = intent.getStringExtra("title"); String content = intent.getStringExtra("content"); - String customContent = intent.getStringExtra("customContent"); + String customContent = intent.getStringExtra("custom_content"); params.putString("title", title); params.putString("content", content); params.putString("custom_content", customContent); @@ -84,16 +84,17 @@ public void onReceive(Context context, Intent intent) { case Constants.ACTION_ON_NOTIFICATION_SHOWED: params.putString("title", intent.getStringExtra("title")); params.putString("content", intent.getStringExtra("content")); - params.putString("custom_content", intent.getStringExtra("customContent")); + params.putString("custom_content", intent.getStringExtra("custom_content")); sendEvent(Constants.EVENT_REMOTE_NOTIFICATION_RECEIVED, params); break; case Constants.ACTION_ON_NOTIFICATION_CLICKED: params.putString("title", intent.getStringExtra("title")); params.putString("content", intent.getStringExtra("content")); - params.putString("custom_content", intent.getStringExtra("customContent")); + params.putString("custom_content", intent.getStringExtra("custom_content")); + params.putBoolean("clicked", true); - sendEvent(Constants.ACTION_ON_NOTIFICATION_CLICKED, params); + sendEvent(Constants.EVENT_REMOTE_NOTIFICATION_RECEIVED, params); break; default: break; @@ -169,7 +170,7 @@ public void onFail(Object data, int errCode, String msg) { * @param promise */ @ReactMethod - public void unregisterPush(final Promise promise) { + public void unRegisterPush(final Promise promise) { XGPushManager.unregisterPush(this.reactContext, new XGIOperateCallback() { @Override public void onSuccess(Object data, int flag) { @@ -295,9 +296,11 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, @Override public void onNewIntent(Intent intent) { + /* Activity activity = getCurrentActivity(); if (activity != null) { - activity.setIntent(intent); // 必须要调用这句 + activity.setIntent(intent); // 后台运行时点击通知会调用 } + */ } } diff --git a/android/src/main/java/com/jeepeng/react/xgpush/receiver/MessageReceiver.java b/android/src/main/java/com/jeepeng/react/xgpush/receiver/MessageReceiver.java index 669fd9e..5e35112 100644 --- a/android/src/main/java/com/jeepeng/react/xgpush/receiver/MessageReceiver.java +++ b/android/src/main/java/com/jeepeng/react/xgpush/receiver/MessageReceiver.java @@ -84,22 +84,30 @@ public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) /** * 通知被打开触发的结果 * @param context - * @param message + * @param notification */ @Override - public void onNotifactionClickedResult(Context context, XGPushClickedResult message) { - if (context == null || message == null) { + public void onNotifactionClickedResult(Context context, XGPushClickedResult notification) { + if (context == null || notification == null) { return; } - if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) { + if (notification.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) { // 通知在通知栏被点击啦。。。。。 Intent intent = new Intent(Constants.ACTION_ON_NOTIFICATION_CLICKED); Bundle bundle = new Bundle(); - bundle.putString("content", message.getContent()); - bundle.putString("title", message.getTitle()); + bundle.putString("content", notification.getContent()); + bundle.putString("title", notification.getTitle()); + bundle.putString("custom_content", notification.getCustomContent()); intent.putExtra("notification", bundle); + + intent.putExtra("title", notification.getTitle()); + intent.putExtra("content", notification.getContent()); + intent.putExtra("custom_content", notification.getCustomContent()); + intent.putExtra("activity", notification.getActivityName()); + intent.putExtra("msgId", notification.getMsgId()); + intent.putExtra("notificationActionType", notification.getNotificationActionType()); context.sendBroadcast(intent); - } else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) { + } else if (notification.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) { // 通知被清除啦。。。。 // APP自己处理通知被清除后的相关动作 } @@ -116,12 +124,12 @@ public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPush Bundle bundle = new Bundle(); bundle.putString("content", xgPushShowedResult.getContent()); bundle.putString("title", xgPushShowedResult.getTitle()); - bundle.putString("customContent", xgPushShowedResult.getCustomContent()); + bundle.putString("custom_content", xgPushShowedResult.getCustomContent()); intent.putExtra("notification", bundle); intent.putExtra("title", xgPushShowedResult.getTitle()); intent.putExtra("content", xgPushShowedResult.getContent()); - intent.putExtra("customContent", xgPushShowedResult.getCustomContent()); + intent.putExtra("custom_content", xgPushShowedResult.getCustomContent()); intent.putExtra("activity", xgPushShowedResult.getActivity()); intent.putExtra("msgId", xgPushShowedResult.getMsgId()); intent.putExtra("notificationId", xgPushShowedResult.getNotifactionId()); diff --git a/example/app/index.js b/example/app/index.js index 9e98a78..bad50eb 100644 --- a/example/app/index.js +++ b/example/app/index.js @@ -93,7 +93,11 @@ class Example extends Component { * @private */ _onNotification(notification) { - alert(JSON.stringify(notification)); + if(notification.clicked === true) { + alert('app处于后台时收到通知' + JSON.stringify(notification)); + } else { + alert('app处于前台时收到通知' + JSON.stringify(notification)); + } } /** diff --git a/example/ios/example/AppDelegate.m b/example/ios/example/AppDelegate.m index e844ebd..5a67ea8 100644 --- a/example/ios/example/AppDelegate.m +++ b/example/ios/example/AppDelegate.m @@ -13,17 +13,6 @@ #import #import -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - -#import -@interface AppDelegate() -@end -#endif - -@interface AppDelegate () - -@end - @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions @@ -44,6 +33,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; // 统计从推送打开的设备 + NSLog(@"%@",launchOptions); [XGPushManager handleLaunching:launchOptions successCallback:^{ NSLog(@"[XGPush] Handle launching success"); } errorCallback:^{ @@ -62,13 +52,20 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio { [XGPushManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } + // Required for the notification event. You must call the completion handler after handling the remote notification. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { - [XGPushManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; + UIApplicationState state = [application applicationState]; + BOOL isClicked = (state != UIApplicationStateActive); + NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:userInfo]; + if(isClicked) { + remoteNotification[@"clicked"] = @YES; + } + [XGPushManager didReceiveRemoteNotification:remoteNotification fetchCompletionHandler:completionHandler]; // 统计收到推送的设备 - [XGPushManager handleReceiveNotification:userInfo successCallback:^{ + [XGPushManager handleReceiveNotification:remoteNotification successCallback:^{ NSLog(@"[XGPush] Handle receive success"); } errorCallback:^{ NSLog(@"[XGPush] Handle receive error"); diff --git a/index.js b/index.js index 8684831..f859baa 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ class XGPush { if (Platform.OS === 'ios') { return XGPushManager.unRegisterDevice(); } else { - return XGPushManager.unregisterPush(); + return XGPushManager.unRegisterPush(); } } diff --git a/package.json b/package.json index 3992c5a..4586b22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-xinge-push", - "version": "0.1.3", + "version": "0.1.4", "description": "Xinge push for react-native", "main": "index.js", "scripts": {