From 49acf24f9aab73a6d014049f4a5e60ca00011813 Mon Sep 17 00:00:00 2001 From: raoxudong Date: Wed, 25 Sep 2019 17:54:45 +0800 Subject: [PATCH] v0.2.0 --- .packages | 2 +- CHANGELOG.md | 3 + README.md | 9 ++- android/src/main/AndroidManifest.xml | 1 + .../java/com/jiguang/jpush/JPushPlugin.java | 64 ++++++++++++++++++- example/ios/Runner.xcodeproj/project.pbxproj | 30 +++++---- example/pubspec.lock | 2 +- ios/Classes/JPushPlugin.m | 28 +++++++- lib/jpush_flutter.dart | 46 +++++++++++-- pubspec.yaml | 2 +- 10 files changed, 162 insertions(+), 25 deletions(-) diff --git a/.packages b/.packages index 7ad6ec98..6368a38b 100644 --- a/.packages +++ b/.packages @@ -1,4 +1,4 @@ -# Generated by pub on 2019-08-23 15:35:48.440553. +# Generated by pub on 2019-09-25 15:21:45.301321. analyzer:file:///Applications/flutter/.pub-cache/hosted/pub.flutter-io.cn/analyzer-0.37.0/lib/ args:file:///Applications/flutter/.pub-cache/hosted/pub.flutter-io.cn/args-1.5.2/lib/ async:file:///Applications/flutter/.pub-cache/hosted/pub.flutter-io.cn/async-2.3.0/lib/ diff --git a/CHANGELOG.md b/CHANGELOG.md index c75dfeab..2771b1c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.2.0 ++ 适配最新版本 JPush SDK ++ Android 支持设置角标 badge ## 0.1.0 + 修复:调用 sendLocalNotification 接口 crash 问题; + 修复:iOS 启动 APP 角标自动消失问题; diff --git a/README.md b/README.md index 66c79a41..b83dd6d8 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,14 @@ ```yaml dependencies: - jpush_flutter: 0.1.0 + jpush_flutter: 0.2.0 + +//github +dependencies: + jmessage_flutter: + git: + url: git://github.com/jpush/jmessage-flutter-plugin.git + ref: master ``` ### 配置 diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index be92956a..6679a91f 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -34,4 +34,5 @@ + diff --git a/android/src/main/java/com/jiguang/jpush/JPushPlugin.java b/android/src/main/java/com/jiguang/jpush/JPushPlugin.java index 511cb48a..aff313c4 100644 --- a/android/src/main/java/com/jiguang/jpush/JPushPlugin.java +++ b/android/src/main/java/com/jiguang/jpush/JPushPlugin.java @@ -33,6 +33,7 @@ public static void registerWith(Registrar registrar) { } + private static String TAG = "| JPUSH | Android | "; public static JPushPlugin instance; static List> openNotificationCache = new ArrayList<>(); @@ -60,6 +61,7 @@ private JPushPlugin(Registrar registrar, MethodChannel channel) { @Override public void onMethodCall(MethodCall call, Result result) { + Log.i(TAG,call.method); if (call.method.equals("getPlatformVersion")) { result.success("Android " + android.os.Build.VERSION.RELEASE); } else if (call.method.equals("setup")) { @@ -90,12 +92,17 @@ public void onMethodCall(MethodCall call, Result result) { getRegistrationID(call, result); } else if (call.method.equals("sendLocalNotification")) { sendLocalNotification(call, result); - } else { + } else if (call.method.equals("setBadge")) { + setBadge(call, result); + } + else { result.notImplemented(); } } public void setup(MethodCall call, Result result) { + Log.d(TAG,"setup :" + call.arguments); + HashMap map = call.arguments(); boolean debug = (boolean)map.get("debug"); JPushInterface.setDebugMode(debug); @@ -112,6 +119,8 @@ public void setup(MethodCall call, Result result) { } public void scheduleCache() { + Log.d(TAG,"scheduleCache:"); + if (dartIsReady) { // try to shedule notifcation cache for (Map notification: JPushPlugin.openNotificationCache) { @@ -131,6 +140,8 @@ public void scheduleCache() { } public void setTags(MethodCall call, Result result) { + Log.d(TAG,"setTags:"); + ListtagList = call.arguments(); Set tags = new HashSet<>(tagList); sequence += 1; @@ -139,12 +150,16 @@ public void setTags(MethodCall call, Result result) { } public void cleanTags(MethodCall call, Result result) { + Log.d(TAG,"cleanTags:"); + sequence += 1; callbackMap.put(sequence, result); JPushInterface.cleanTags(registrar.context(), sequence); } public void addTags(MethodCall call, Result result) { + Log.d(TAG,"addTags: " + call.arguments); + ListtagList = call.arguments(); Set tags = new HashSet<>(tagList); sequence += 1; @@ -153,6 +168,8 @@ public void addTags(MethodCall call, Result result) { } public void deleteTags(MethodCall call, Result result) { + Log.d(TAG,"deleteTags: " + call.arguments); + ListtagList = call.arguments(); Set tags = new HashSet<>(tagList); sequence += 1; @@ -161,12 +178,16 @@ public void deleteTags(MethodCall call, Result result) { } public void getAllTags(MethodCall call, Result result) { + Log.d(TAG,"getAllTags: "); + sequence += 1; callbackMap.put(sequence, result); JPushInterface.getAllTags(registrar.context(), sequence); } public void setAlias(MethodCall call, Result result) { + Log.d(TAG,"setAlias: " + call.arguments); + String alias= call.arguments(); sequence += 1; callbackMap.put(sequence, result); @@ -174,6 +195,8 @@ public void setAlias(MethodCall call, Result result) { } public void deleteAlias(MethodCall call, Result result) { + Log.d(TAG,"deleteAlias:"); + String alias= call.arguments(); sequence += 1; callbackMap.put(sequence, result); @@ -181,22 +204,31 @@ public void deleteAlias(MethodCall call, Result result) { } public void stopPush(MethodCall call, Result result) { + Log.d(TAG,"stopPush:"); + JPushInterface.stopPush(registrar.context()); } public void resumePush(MethodCall call, Result result) { + Log.d(TAG,"resumePush:"); + JPushInterface.resumePush(registrar.context()); } public void clearAllNotifications(MethodCall call, Result result) { + Log.d(TAG,"clearAllNotifications: "); + JPushInterface.clearAllNotifications(registrar.context()); } public void getLaunchAppNotification(MethodCall call, Result result) { + Log.d(TAG,""); + } public void getRegistrationID(MethodCall call, Result result) { + Log.d(TAG,"getRegistrationID: "); String rid = JPushInterface.getRegistrationID(registrar.context()); if (rid == null || rid.isEmpty()) { @@ -208,6 +240,8 @@ public void getRegistrationID(MethodCall call, Result result) { public void sendLocalNotification(MethodCall call, Result result) { + Log.d(TAG,"sendLocalNotification: " + call.arguments); + try { HashMap map = call.arguments(); @@ -232,6 +266,17 @@ public void sendLocalNotification(MethodCall call, Result result) { } } + public void setBadge(MethodCall call, Result result) { + Log.d(TAG,"setBadge: " + call.arguments); + + HashMap map = call.arguments(); + Object numObject = map.get("badge"); + if (numObject != null) { + int num = (int)numObject; + JPushInterface.setBadgeNumber(registrar.context(),num); + result.success(true); + } + } /** * 接收自定义消息,通知,通知点击事件等事件的广播 @@ -248,6 +293,7 @@ public JPushReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); + if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) { String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID); Log.d("JPushPlugin","on get registration"); @@ -264,12 +310,16 @@ public void onReceive(Context context, Intent intent) { } private void handlingMessageReceive(Intent intent) { + Log.d(TAG,"handlingMessageReceive " + intent.getAction()); + String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE); Map extras = getNotificationExtras(intent); JPushPlugin.transmitMessageReceive(msg, extras); } private void handlingNotificationOpen(Context context, Intent intent) { + Log.d(TAG,"handlingNotificationOpen " + intent.getAction()); + String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); Map extras = getNotificationExtras(intent); @@ -284,6 +334,8 @@ private void handlingNotificationOpen(Context context, Intent intent) { } private void handlingNotificationReceive(Context context, Intent intent) { + Log.d(TAG,"handlingNotificationReceive " + intent.getAction()); + String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE); String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT); Map extras = getNotificationExtras(intent); @@ -291,6 +343,8 @@ private void handlingNotificationReceive(Context context, Intent intent) { } private Map getNotificationExtras(Intent intent) { + Log.d(TAG,""); + Map extrasMap = new HashMap(); for (String key : intent.getExtras().keySet()) { if (!IGNORED_EXTRAS_KEYS.contains(key)) { @@ -307,6 +361,8 @@ private Map getNotificationExtras(Intent intent) { static void transmitMessageReceive(String message, Map extras) { + Log.d(TAG,"transmitMessageReceive " + "message=" + message + "extras=" + extras); + if (instance == null) { return; } @@ -318,6 +374,8 @@ static void transmitMessageReceive(String message, Map extras) { } static void transmitNotificationOpen(String title, String alert, Map extras) { + Log.d(TAG,"transmitNotificationOpen " + "title=" + title + "alert=" + alert + "extras=" + extras); + Map notification= new HashMap<>(); notification.put("title", title); notification.put("alert", alert); @@ -338,6 +396,8 @@ static void transmitNotificationOpen(String title, String alert, Map extras) { + Log.d(TAG,"transmitNotificationReceive " + "title=" + title + "alert=" + alert + "extras=" + extras); + if (instance == null) { return; } @@ -350,6 +410,8 @@ static void transmitNotificationReceive(String title, String alert, Map +#define JPLog(fmt, ...) NSLog((@"| JPUSH | iOS | " fmt), ##__VA_ARGS__) + @interface NSError (FlutterError) @property(readonly, nonatomic) FlutterError *flutterError; @end @@ -111,6 +113,8 @@ - (void)networkDidReceiveMessage:(NSNotification *)notification { } - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"handleMethodCall:%@",call.method); + if ([@"getPlatformVersion" isEqualToString:call.method]) { result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]); } else if([@"setup" isEqualToString:call.method]) { @@ -153,6 +157,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)setup:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"setup:"); NSDictionary *arguments = call.arguments; NSNumber *debug = arguments[@"debug"]; if ([debug boolValue]) { @@ -168,6 +173,7 @@ - (void)setup:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)applyPushAuthority:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"applyPushAuthority:%@",call.arguments); notificationTypes = 0; NSDictionary *arguments = call.arguments; if ([arguments[@"sound"] boolValue]) { @@ -185,6 +191,7 @@ - (void)applyPushAuthority:(FlutterMethodCall*)call result:(FlutterResult)result } - (void)setTags:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"setTags:%@",call.arguments); NSSet *tagSet; if (call.arguments != NULL) { @@ -202,6 +209,7 @@ - (void)setTags:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)cleanTags:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"cleanTags:"); [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) { if (iResCode == 0) { result(@{@"tags": iTags ? [iTags allObjects] : @[]}); @@ -213,6 +221,7 @@ - (void)cleanTags:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)addTags:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"addTags:%@",call.arguments); NSSet *tagSet; if (call.arguments != NULL) { @@ -230,6 +239,7 @@ - (void)addTags:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)deleteTags:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"deleteTags:%@",call.arguments); NSSet *tagSet; if (call.arguments != NULL) { @@ -247,6 +257,7 @@ - (void)deleteTags:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)getAllTags:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"getAllTags:"); [JPUSHService getAllTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) { if (iResCode == 0) { result(@{@"tags": iTags ? [iTags allObjects] : @[]}); @@ -258,6 +269,7 @@ - (void)getAllTags:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)setAlias:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"setAlias:%@",call.arguments); NSString *alias = call.arguments; [JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) { if (iResCode == 0) { @@ -270,6 +282,7 @@ - (void)setAlias:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)deleteAlias:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"deleteAlias:%@",call.arguments); [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) { if (iResCode == 0) { result(@{@"alias": iAlias ?: @""}); @@ -281,24 +294,32 @@ - (void)deleteAlias:(FlutterMethodCall*)call result:(FlutterResult)result { } - (void)setBadge:(FlutterMethodCall*)call result:(FlutterResult)result { - NSNumber *badge = call.arguments; - [[UIApplication sharedApplication] setApplicationIconBadgeNumber: badge.integerValue]; - [JPUSHService setBadge: badge.integerValue > 0 ? badge.integerValue: 0]; + JPLog(@"setBadge:%@",call.arguments); + NSInteger badge = [call.arguments[@"badge"] integerValue]; + if (badge < 0) { + badge = 0; + } + [[UIApplication sharedApplication] setApplicationIconBadgeNumber: badge]; + [JPUSHService setBadge: badge]; } - (void)stopPush:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"stopPush:"); [[UIApplication sharedApplication] unregisterForRemoteNotifications]; } - (void)clearAllNotifications:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"clearAllNotifications:"); [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; } - (void)getLaunchAppNotification:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"getLaunchAppNotification"); result(_launchNotification == nil ? @{}: _launchNotification); } - (void)getRegistrationID:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"getRegistrationID:"); #if TARGET_IPHONE_SIMULATOR//模拟器 NSLog(@"simulator can not get registrationid"); result(@""); @@ -320,6 +341,7 @@ - (void)getRegistrationID:(FlutterMethodCall*)call result:(FlutterResult)result } - (void)sendLocalNotification:(FlutterMethodCall*)call result:(FlutterResult)result { + JPLog(@"sendLocalNotification:%@",call.arguments); JPushNotificationContent *content = [[JPushNotificationContent alloc] init]; NSDictionary *params = call.arguments; if (params[@"title"]) { diff --git a/lib/jpush_flutter.dart b/lib/jpush_flutter.dart index 962551c8..52e72c45 100644 --- a/lib/jpush_flutter.dart +++ b/lib/jpush_flutter.dart @@ -7,6 +7,7 @@ import 'package:platform/platform.dart'; typedef Future EventHandler(Map event); class JPush { + final String flutter_log = "| JPUSH | Flutter | "; factory JPush() => _instance; final MethodChannel _channel; @@ -31,6 +32,8 @@ class JPush { String channel = '', bool debug = false, }) { + print(flutter_log + "setup:"); + _channel.invokeMethod('setup', { 'appKey': appKey, 'channel': channel, 'production': production, 'debug': debug}); } /// @@ -41,6 +44,8 @@ class JPush { EventHandler onOpenNotification, EventHandler onReceiveMessage, }) { + print(flutter_log + "addEventHandler:"); + _onReceiveNotification = onReceiveNotification; _onOpenNotification = onOpenNotification; _onReceiveMessage = onReceiveMessage; @@ -48,6 +53,8 @@ class JPush { } Future _handleMethod(MethodCall call) async { + print(flutter_log + "_handleMethod:"); + switch (call.method) { case "onReceiveNotification": return _onReceiveNotification(call.arguments.cast()); @@ -64,8 +71,9 @@ class JPush { /// 申请推送权限,注意这个方法只会向用户弹出一次推送权限请求(如果用户不同意,之后只能用户到设置页面里面勾选相应权限),需要开发者选择合适的时机调用。 /// void applyPushAuthority([NotificationSettingsIOS iosSettings = const NotificationSettingsIOS()]) { + print(flutter_log + "applyPushAuthority:"); - if (!_platform.isIOS) { + if (!_platform.isIOS) { return; } @@ -80,6 +88,8 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> setTags(List tags) async { + print(flutter_log + "setTags:"); + final Map result = await _channel.invokeMethod('setTags', tags); return result; } @@ -91,6 +101,8 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> cleanTags() async { + print(flutter_log + "cleanTags:"); + final Map result = await _channel.invokeMethod('cleanTags'); return result; } @@ -104,6 +116,8 @@ class JPush { /// Future> addTags(List tags) async { + print(flutter_log + "addTags:"); + final Map result = await _channel.invokeMethod('addTags', tags); return result; } @@ -116,6 +130,8 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> deleteTags(List tags) async { + print(flutter_log + "deleteTags:"); + final Map result = await _channel.invokeMethod('deleteTags', tags); return result; } @@ -127,6 +143,8 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> getAllTags() async { + print(flutter_log + "getAllTags:"); + final Map result = await _channel.invokeMethod('getAllTags'); return result; } @@ -140,6 +158,8 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> setAlias(String alias) async { + print(flutter_log + "setAlias:"); + final Map result = await _channel.invokeMethod('setAlias', alias); return result; } @@ -151,24 +171,31 @@ class JPush { /// @param {Function} fail = ({"errorCode":int}) => { } /// Future> deleteAlias() async { + print(flutter_log + "deleteAlias:"); + final Map result = await _channel.invokeMethod('deleteAlias'); return result; } /// - /// iOS Only /// 设置应用 Badge(小红点) /// /// @param {Int} badge /// + /// 注意:如果是 Android 手机,目前仅支持华为手机 + /// Future setBadge(int badge) async { - await _channel.invokeMethod('setBadge', badge); + print(flutter_log + "setBadge:"); + + await _channel.invokeMethod('setBadge', {"badge":badge}); } /// /// 停止接收推送,调用该方法后应用将不再受到推送,如果想要重新收到推送可以调用 resumePush。 /// Future stopPush() async { + print(flutter_log + "stopPush:"); + await _channel.invokeMethod('stopPush'); } @@ -176,6 +203,8 @@ class JPush { /// 恢复推送功能。 /// Future resumePush() async { + print(flutter_log + "resumePush:"); + await _channel.invokeMethod('resumePush'); } @@ -183,6 +212,8 @@ class JPush { /// 清空通知栏上的所有通知。 /// Future clearAllNotifications() async { + print(flutter_log + "clearAllNotifications:"); + await _channel.invokeMethod('clearAllNotifications'); } @@ -194,6 +225,8 @@ class JPush { /// @param {Function} callback = (Object) => {} /// Future> getLaunchAppNotification() async { + print(flutter_log + "getLaunchAppNotification:"); + final Map result = await _channel.invokeMethod('getLaunchAppNotification'); return result; } @@ -204,6 +237,8 @@ class JPush { /// @param {Function} callback = (String) => {} /// Future getRegistrationID() async { + print(flutter_log + "getRegistrationID:"); + final String rid = await _channel.invokeMethod('getRegistrationID'); return rid; } @@ -213,10 +248,13 @@ class JPush { /// @param {Notification} notification /// Future sendLocalNotification(LocalNotification notification) async { - await _channel.invokeMethod('sendLocalNotification', notification.toMap()); + print(flutter_log + "sendLocalNotification:"); + + await _channel.invokeMethod('sendLocalNotification', notification.toMap()); return notification.toMap().toString(); } + } class NotificationSettingsIOS { diff --git a/pubspec.yaml b/pubspec.yaml index 0be7aa97..88c31b67 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: jpush_flutter description: Offically supported JPush Flutter plugin. -version: 0.1.0 +version: 0.2.0 author: huminios homepage: https://www.jiguang.cn