diff --git a/README.md b/README.md index ddeef61..982023c 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ react-native link react-native-xinge-push 5. 再次进行单推/全推,如果能够收到推送,则表明厂商通道集成成功 ###### 注意事项 +消息目前将其理解为两类:静默消息和普通通知 +静默消息不会弹窗,通知会弹窗 + 如果在EMUI 8.0(Android 8)上,出现发通知成功但通知栏不显示的情况,并在Logcat看到以下错误: ``` E/NotificationService: No Channel found for pkg=com.jeepeng.push, channelId=null, id=995033369, tag=null, opPkg=com.huawei.android.pushagent, callingUid=10060, userId=0, incomingUserId=0, notificationUid=10261, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=default tick defaults=0x1 flags=0x10 color=0x00000000 vis=PRIVATE) diff --git a/android/build.gradle b/android/build.gradle index 18a55d7..fe6236d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -15,7 +15,7 @@ android { manifestPlaceholders = [ XG_ACCESS_ID: "", XG_ACCESS_KEY: "", - HW_APPID: "", + HW_APPID: "" ] } diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 557aa4b..40f5f20 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -14,45 +14,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 776a0e2..71aedc4 100644 --- a/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java +++ b/android/src/main/java/com/jeepeng/react/xgpush/PushModule.java @@ -52,9 +52,11 @@ public String getName() { } private void sendEvent(String eventName, @Nullable WritableMap params) { - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); + if(getReactApplicationContext().hasActiveCatalystInstance()){ + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, params); + } } private void registerReceivers() { diff --git a/android/src/main/java/com/jeepeng/react/xgpush/receiver/HWMessageReceiver.java b/android/src/main/java/com/jeepeng/react/xgpush/receiver/HWMessageReceiver.java deleted file mode 100644 index e5baf11..0000000 --- a/android/src/main/java/com/jeepeng/react/xgpush/receiver/HWMessageReceiver.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.jeepeng.react.xgpush.receiver; - -import com.tencent.android.hwpush.HWPushMessageReceiver; - -/** - * 华为官方推送通道消息接收器 - * Created by Jeepeng on 2018/3/11. - */ - -public class HWMessageReceiver extends HWPushMessageReceiver { -} \ No newline at end of file diff --git a/android/src/main/java/com/jeepeng/react/xgpush/receiver/MZMessageReceiver.java b/android/src/main/java/com/jeepeng/react/xgpush/receiver/MZMessageReceiver.java deleted file mode 100644 index a22a6cc..0000000 --- a/android/src/main/java/com/jeepeng/react/xgpush/receiver/MZMessageReceiver.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.jeepeng.react.xgpush.receiver; - -import android.content.Context; - -import com.meizu.cloud.pushsdk.MzPushMessageReceiver; -import com.meizu.cloud.pushsdk.platform.message.PushSwitchStatus; -import com.meizu.cloud.pushsdk.platform.message.RegisterStatus; -import com.meizu.cloud.pushsdk.platform.message.SubAliasStatus; -import com.meizu.cloud.pushsdk.platform.message.SubTagsStatus; -import com.meizu.cloud.pushsdk.platform.message.UnRegisterStatus; - -/** - * 魅族官方推送通道消息接收器 - * Created by Jeepeng on 2018/3/12. - */ - -public class MZMessageReceiver extends MzPushMessageReceiver { - @Override - public void onRegister(Context context, String s) { - - } - - @Override - public void onMessage(Context context, String s) { - - } - - @Override - public void onUnRegister(Context context, boolean b) { - - } - - @Override - public void onPushStatus(Context context, PushSwitchStatus pushSwitchStatus) { - - } - - @Override - public void onRegisterStatus(Context context, RegisterStatus registerStatus) { - - } - - @Override - public void onUnRegisterStatus(Context context, UnRegisterStatus unRegisterStatus) { - - } - - @Override - public void onSubTagsStatus(Context context, SubTagsStatus subTagsStatus) { - - } - - @Override - public void onSubAliasStatus(Context context, SubAliasStatus subAliasStatus) { - - } -} 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 18ebb2f..b8f2307 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 @@ -102,10 +102,6 @@ public void onNotifactionClickedResult(Context context, XGPushClickedResult noti 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()); diff --git a/android/src/main/java/com/jeepeng/react/xgpush/receiver/XMMessageReceiver.java b/android/src/main/java/com/jeepeng/react/xgpush/receiver/XMMessageReceiver.java deleted file mode 100644 index 5aa068e..0000000 --- a/android/src/main/java/com/jeepeng/react/xgpush/receiver/XMMessageReceiver.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jeepeng.react.xgpush.receiver; - -import android.content.Context; - -import com.xiaomi.mipush.sdk.MiPushCommandMessage; -import com.xiaomi.mipush.sdk.MiPushMessage; -import com.xiaomi.mipush.sdk.PushMessageReceiver; - -/** - * 小米官方推送通道消息接收器 - * Created by Jeepeng on 2018/3/12. - */ - -public class XMMessageReceiver extends PushMessageReceiver { - public void onReceivePassThroughMessage(Context context, MiPushMessage message) { - } - - public void onNotificationMessageClicked(Context context, MiPushMessage message) { - } - - public void onNotificationMessageArrived(Context context, MiPushMessage message) { - } - - public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) { - } - - public void onCommandResult(Context context, MiPushCommandMessage message) { - } -} diff --git a/example/app/index.js b/example/app/index.js index 517381c..8eb34e7 100644 --- a/example/app/index.js +++ b/example/app/index.js @@ -32,6 +32,7 @@ class Example extends Component { } initPush() { + XGPush.enableDebug(true) // 初始化 if(Platform.OS === 'android') { // 请将1111111111修改为APP的AccessId,10位数字 @@ -93,7 +94,7 @@ class Example extends Component { * @private */ _onMessage(message) { - alert('收到透传消息: ' + message.content); + alert('收到透传消息: ' + JSON.stringify(message.content)); } /** @@ -102,6 +103,7 @@ class Example extends Component { * @private */ _onNotification(notification) { + console.log(notification) if(notification.clicked === true) { alert('app处于后台时收到通知' + JSON.stringify(notification)); } else { diff --git a/example/ios/example.xcodeproj/xcshareddata/xcschemes/example.xcscheme b/example/ios/example.xcodeproj/xcshareddata/xcschemes/example.xcscheme index c13a4bf..841adc2 100644 --- a/example/ios/example.xcodeproj/xcshareddata/xcschemes/example.xcscheme +++ b/example/ios/example.xcodeproj/xcshareddata/xcschemes/example.xcscheme @@ -54,6 +54,7 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + language = "" shouldUseLaunchSchemeArgsEnv = "YES"> = __IPHONE_10_0 -// App 用户点击通知 -// App 用户选择通知中的行为 -// App 用户在通知中心清除消息 -// 无论本地推送还是远程推送都会走这个回调 -- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { - NSLog(@"[XGPush] click notification"); - if ([response.actionIdentifier isEqualToString:@"xgaction001"]) { - NSLog(@"click from Action1"); - } else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) { - NSLog(@"click from Action2"); - } - - [[XGPush defaultManager] reportXGNotificationResponse:response]; - - completionHandler(); -} -// App 在前台弹通知需要调用这个接口 -- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { - [[XGPush defaultManager] reportXGNotificationInfo:notification.request.content.userInfo]; - completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); -} -#endif @end diff --git a/index.js b/index.js index 1077d4d..a50071e 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ const EventMapping = Platform.select({ ios: { register: 'remoteNotificationsRegistered', notification: 'remoteNotificationReceived', + message: 'remoteMessageReceived', localNotification: 'localNotificationReceived', }, }); @@ -118,7 +119,7 @@ class XGPush { static addEventListener(eventType, callback) { let event = EventMapping[eventType]; if (!event) { - console.warn('XGPush only supports `notification`, `register` and `localNotification` events'); + console.warn('XGPush only supports `notification`, `register`, `message` ,and `localNotification` events'); return; } let listener = XGNativeEventEmitter.addListener(event, (data) => { @@ -133,7 +134,7 @@ class XGPush { static removeEventListener(eventType, callback) { if (!EventMapping[eventType]) { - console.warn('XGPush only supports `notification`, `register` and `localNotification` events'); + console.warn('XGPush only supports `notification`, `register`, `message` and `localNotification` events'); return; } let listener = _handlers.get(callback); diff --git a/ios/XGPush/XGPushManager.h b/ios/XGPush/XGPushManager.h index 034d48f..490ad73 100644 --- a/ios/XGPush/XGPushManager.h +++ b/ios/XGPush/XGPushManager.h @@ -19,6 +19,8 @@ typedef void (^RCTRemoteNotificationCallback)(UIBackgroundFetchResult result); + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; + (void)didReceiveRemoteNotification:(NSDictionary *)notification; + (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler; ++ (void)didReceiveRemoteMessage:(NSDictionary *)notification; ++ (void)didReceiveRemoteMessage:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler; + (void)didReceiveLocalNotification:(UILocalNotification *)notification; + (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; #endif diff --git a/ios/XGPush/XGPushManager.m b/ios/XGPush/XGPushManager.m index 04f29c3..9804e02 100644 --- a/ios/XGPush/XGPushManager.m +++ b/ios/XGPush/XGPushManager.m @@ -16,6 +16,7 @@ #import NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; +NSString *const RCTRemoteMessageReceived = @"RemoteMessageReceived"; static NSString *const kLocalNotificationReceived = @"LocalNotificationReceived"; static NSString *const kRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; @@ -142,6 +143,11 @@ - (void)startObserving selector:@selector(handleLocalNotificationReceived:) name:kLocalNotificationReceived object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteMessageReceived:) + name:RCTRemoteMessageReceived + object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRemoteNotificationReceived:) name:RCTRemoteNotificationReceived @@ -169,6 +175,7 @@ - (void)stopObserving { return @[@"localNotificationReceived", @"remoteNotificationReceived", + @"remoteMessageReceived", @"remoteNotificationsRegistered", @"remoteNotificationRegistrationError"]; } @@ -203,6 +210,49 @@ + (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error userInfo:@{@"error": error}]; } +// iOS 10 新增 API +// iOS 10 会走新 API, iOS 10 以前会走到老 API +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +// App 用户点击通知 +// App 用户选择通知中的行为 +// App 用户在通知中心清除消息 +// 无论本地推送还是远程推送都会走这个回调 +- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { + UNNotification * notification = response.notification; + + if ([response.actionIdentifier isEqualToString:@"xgaction001"]) { + NSLog(@"click from Action1"); + } else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) { + NSLog(@"click from Action2"); + } + UIApplicationState state = [RCTSharedApplication() applicationState]; + NSLog(@"state = %ld, UIApplicationStateActive = %ld", (long)state, (long)UIApplicationStateActive); + + BOOL isBackground = (state != UIApplicationStateActive); + NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo]; + remoteNotification[@"clicked"] = @YES; + if(isBackground) { + remoteNotification[@"background"] = @YES; + } + NSLog(@"[XGPush] click notification %@", remoteNotification); + NSDictionary * userInfo = @{@"notification": remoteNotification}; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived + object:self + userInfo:userInfo]; + [[XGPush defaultManager] reportXGNotificationResponse:response]; + + completionHandler(); +} + +// App 在前台弹通知需要调用这个接口 +- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { + NSDictionary * userInfo = notification.request.content.userInfo; + NSLog(@"[XGPush] willPresentNotification notification %@", userInfo); + [[XGPush defaultManager] reportXGNotificationInfo:userInfo]; + completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); +} +#endif + + (void)didReceiveRemoteNotification:(NSDictionary *)notification { NSDictionary *userInfo = @{@"notification": notification}; @@ -215,13 +265,33 @@ + (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler { NSDictionary *userInfo = @{@"notification": notification, @"completionHandler": completionHandler}; + NSLog(@"%s, notification is %@", __FUNCTION__, notification); [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived object:self userInfo:userInfo]; } ++ (void)didReceiveRemoteMessage:(NSDictionary *)notification +{ + NSDictionary *userInfo = @{@"notification": notification}; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteMessageReceived + object:self + userInfo:userInfo]; +} + ++ (void)didReceiveRemoteMessage:(NSDictionary *)notification + fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler +{ + NSDictionary *userInfo = @{@"notification": notification, @"completionHandler": completionHandler}; + NSLog(@"%s, notification is %@", __FUNCTION__, notification); + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteMessageReceived + object:self + userInfo:userInfo]; +} + + (void)didReceiveLocalNotification:(UILocalNotification *)notification { + NSLog(@"%s, notification is %@", __FUNCTION__, notification); [[NSNotificationCenter defaultCenter] postNotificationName:kLocalNotificationReceived object:self userInfo:RCTFormatLocalNotification(notification)]; @@ -232,6 +302,25 @@ - (void)handleLocalNotificationReceived:(NSNotification *)notification [self sendEventWithName:@"localNotificationReceived" body:notification.userInfo]; } +- (void)handleRemoteMessageReceived:(NSNotification *)notification +{ + NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionary]; + remoteNotification[@"content"] = notification.userInfo[@"notification"]; + RCTRemoteNotificationCallback completionHandler = notification.userInfo[@"completionHandler"]; + NSString *notificationId = [[NSUUID UUID] UUIDString]; + remoteNotification[@"notificationId"] = notificationId; + remoteNotification[@"remote"] = @YES; + if (completionHandler) { + if (!self.remoteNotificationCallbacks) { + // Lazy initialization + self.remoteNotificationCallbacks = [NSMutableDictionary dictionary]; + } + self.remoteNotificationCallbacks[notificationId] = completionHandler; + } + + [self sendEventWithName:@"remoteMessageReceived" body:remoteNotification]; +} + - (void)handleRemoteNotificationReceived:(NSNotification *)notification { NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:notification.userInfo[@"notification"]]; @@ -263,6 +352,7 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification @"code": @(error.code), @"details": error.userInfo, }; + NSLog(@"%s, notification is %@", __FUNCTION__, notification); [self sendEventWithName:@"remoteNotificationRegistrationError" body:errorDetails]; } @@ -512,7 +602,7 @@ - (void)xgPushDidUnbindWithIdentifier:(NSString *)identifier type:(XGPushTokenBi */ RCT_EXPORT_METHOD(startXGWithAppID:(uint64_t)appID appKey:(nonnull NSString *)appKey) { - [[XGPush defaultManager] startXGWithAppID:appID appKey:appKey delegate:self]; + [[XGPush defaultManager] startXGWithAppID:appID appKey:appKey delegate:self]; } /** diff --git a/package.json b/package.json index 91d9f6c..9355d73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-xinge-push", - "version": "0.7.0", + "version": "0.8.0", "description": "Xinge push for react-native", "main": "index.js", "scripts": { @@ -8,7 +8,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/Jeepeng/react-native-xinge-push.git" + "url": "git+https://github.com/wanxsb/react-native-xinge-push.git" }, "keywords": [ "xinge", @@ -19,7 +19,7 @@ "author": "Jeepeng", "license": "Apache-2.0", "bugs": { - "url": "https://github.com/Jeepeng/react-native-xinge-push/issues" + "url": "https://github.com/wanxsb/react-native-xinge-push/issues" }, - "homepage": "https://github.com/Jeepeng/react-native-xinge-push#readme" + "homepage": "https://github.com/wanxsb/react-native-xinge-push#readme" }