Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
app运行时,notification 加入 clicked 属性,标记通知是否被点击
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeepeng committed Jun 9, 2017
1 parent 67c0e9c commit c6f8624
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
15 changes: 9 additions & 6 deletions android/src/main/java/com/jeepeng/react/xgpush/PushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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); // 后台运行时点击通知会调用
}
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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自己处理通知被清除后的相关动作
}
Expand All @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion example/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down
23 changes: 10 additions & 13 deletions example/ios/example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate() <UNUserNotificationCenterDelegate>
@end
#endif

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Expand All @@ -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:^{
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class XGPush {
if (Platform.OS === 'ios') {
return XGPushManager.unRegisterDevice();
} else {
return XGPushManager.unregisterPush();
return XGPushManager.unRegisterPush();
}

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit c6f8624

Please sign in to comment.