From a545e7881f80648fa3427b85258afc2459478b1b Mon Sep 17 00:00:00 2001 From: huangminlinux <380108184@qq.com> Date: Sat, 29 Sep 2018 10:14:06 +0800 Subject: [PATCH] add localnotification api --- .packages | 2 +- .../java/com/jiguang/jpush/JPushPlugin.java | 31 +++++++- example/android/gradle.properties | 19 +++++ example/ios/Runner.xcodeproj/project.pbxproj | 4 +- .../xcshareddata/WorkspaceSettings.xcsettings | 8 +++ example/lib/main.dart | 53 +++++++++++--- example/pubspec.lock | 2 +- ios/Classes/JPushPlugin.m | 72 ++++++++++++++++++- lib/jpush_flutter.dart | 70 ++++++++++++++++++ 9 files changed, 243 insertions(+), 18 deletions(-) create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/.packages b/.packages index 1b0be92..a27bb8b 100644 --- a/.packages +++ b/.packages @@ -1,4 +1,4 @@ -# Generated by pub on 2018-09-18 16:05:14.738810. +# Generated by pub on 2018-09-19 16:23:17.855023. collection:file:///Applications/flutter/.pub-cache/hosted/pub.dartlang.org/collection-1.14.11/lib/ flutter:file:///Applications/flutter/packages/flutter/lib/ meta:file:///Applications/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.1.6/lib/ diff --git a/android/src/main/java/com/jiguang/jpush/JPushPlugin.java b/android/src/main/java/com/jiguang/jpush/JPushPlugin.java index 08d62c9..85d9272 100644 --- a/android/src/main/java/com/jiguang/jpush/JPushPlugin.java +++ b/android/src/main/java/com/jiguang/jpush/JPushPlugin.java @@ -5,6 +5,9 @@ import android.content.Context; import android.content.Intent; import android.util.Log; +import org.json.JSONObject; + +import cn.jpush.android.data.JPushLocalNotification; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; @@ -85,6 +88,8 @@ public class JPushPlugin implements MethodCallHandler { getLaunchAppNotification(call, result); } else if (call.method.equals("getRegistrationID")) { getRegistrationID(call, result); + } else if (call.method.equals("sendLocalNotification")) { + sendLocalNotification(call, result); } else { result.notImplemented(); } @@ -196,6 +201,31 @@ public class JPushPlugin implements MethodCallHandler { } + public void sendLocalNotification(MethodCall call, Result result) { + try { + HashMap map = call.arguments(); + Log.d("JPushPlugin", "1111111111111111111"); +// Log.d("JPushPlugin", map.toString()); + JPushLocalNotification ln = new JPushLocalNotification(); + ln.setBuilderId((Integer)map.get("buildId")); + ln.setNotificationId((Integer)map.get("id")); + ln.setTitle((String) map.get("title")); + ln.setContent((String) map.get("content")); + HashMap extra = (HashMap)map.get("extra"); + + if (extra != null) { + JSONObject json = new JSONObject(extra); + ln.setExtras(json.toString()); + } + + long date = (long) map.get("fireTime"); + ln.setBroadcastTime(date); + + JPushInterface.addLocalNotification(registrar.context(), ln); + } catch (Exception e) { + e.printStackTrace(); + } + } @@ -328,5 +358,4 @@ public class JPushPlugin implements MethodCallHandler { JPushPlugin.instance.scheduleCache(); } - } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 8bd86f6..fc9dfa6 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1 +1,20 @@ +## Project-wide Gradle settings. +# +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx1024m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +# +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +#Wed Sep 19 16:44:40 CST 2018 +systemProp.http.proxyHost=127.0.0.1 +systemProp.https.proxyPort=1086 org.gradle.jvmargs=-Xmx1536M +systemProp.https.proxyHost=127.0.0.1 +systemProp.http.proxyPort=1086 diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 9edf29e..f04deb8 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -240,7 +240,7 @@ ); inputPaths = ( "${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", - "${PODS_ROOT}/../.symlinks/flutter/ios-release/Flutter.framework", + "${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( @@ -263,7 +263,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin"; + shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin\n"; }; 7A418C368FF000006114CEC1 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..949b678 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + BuildSystemType + Original + + diff --git a/example/lib/main.dart b/example/lib/main.dart index 0e69db2..ee68dd7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -25,11 +25,11 @@ class _MyAppState extends State { String platformVersion; // Platform messages may fail, so we use a try/catch PlatformException. JPush.getRegistrationID().then((rid) { - setState(() { - _platformVersion = "flutter getRegistrationID: $rid"; - }); + // setState(() { + // _platformVersion = "flutter getRegistrationID: $rid"; + // }); }); - + JPush.setup( appKey: "a1703c14b186a68a66ef86c1", channel: "theChannel", @@ -45,13 +45,15 @@ class _MyAppState extends State { JPush.addEventHandler( onReceiveNotification: (Map message) async { // print("flutter onReceiveNotification: $message"); - setState(() { - _platformVersion = "setAlias error: $message"; - }); + // setState(() { + // _platformVersion = "flutter onReceiveNotification: $message"; + // }); }, onOpenNotification: (Map message) async { print("flutter onOpenNotification: $message"); - + setState(() { + _platformVersion = "flutter onOpenNotification: $message"; + }); }, onReceiveMessage: (Map message) async { print("flutter onReceiveMessage: $message"); @@ -90,11 +92,42 @@ class _MyAppState extends State { child: new Column( children:[ new Text('result: $_platformVersion\n'), - + new FlatButton( + child: new Text('sendLocalNotification\n'), + onPressed: () { + // @require this.id, + // @require this.title, + // @require this.content, + // @require this.fireTime, + // this.buildId, + // this.extras, + // this.badge, + // this.soundName, + // this.subtitle + // 三秒后出发本地推送 + var fireDate = DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch + 3000); + var localNotification = LocalNotification( + id: 234, + title: 'fadsfa', + buildId: 1, + content: 'fdas', + fireTime: fireDate, + subtitle: 'fasf', + badge: 5, + extras: {"fa": 0} + ); + JPush.sendLocalNotification(localNotification).then((res) { + setState(() { + _platformVersion = res; + }); + }); + + }), + new FlatButton( child: new Text('applyPushAuthority\n'), onPressed: () { - + JPush.applyPushAuthority(NotificationSettingsIOS(badge: true, alert: true, sound: true)); }), new FlatButton( child: new Text('setTags\n'), diff --git a/example/pubspec.lock b/example/pubspec.lock index 30fa3aa..ef1c983 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -136,7 +136,7 @@ packages: path: ".." relative: true source: path - version: "0.0.1" + version: "0.0.2" js: dependency: transitive description: diff --git a/ios/Classes/JPushPlugin.m b/ios/Classes/JPushPlugin.m index 15d3748..bf6c7bb 100644 --- a/ios/Classes/JPushPlugin.m +++ b/ios/Classes/JPushPlugin.m @@ -143,9 +143,9 @@ static NSMutableArray* getRidResults; [self getLaunchAppNotification:call result:result]; } else if([@"getRegistrationID" isEqualToString:call.method]) { [self getRegistrationID:call result:result]; - } - - else{ + } else if([@"sendLocalNotification"isEqualToString:call.method]) { + [self sendLocalNotification:call result:result]; + } else{ result(FlutterMethodNotImplemented); } } @@ -303,6 +303,7 @@ static NSMutableArray* getRidResults; result(@""); #elif TARGET_OS_IPHONE//真机 + if ([JPUSHService registrationID] != nil && ![[JPUSHService registrationID] isEqualToString:@""]) { // 如果已经成功获取 registrationID,从本地获取直接缓存 result([JPUSHService registrationID]); @@ -317,6 +318,71 @@ static NSMutableArray* getRidResults; #endif } +- (void)sendLocalNotification:(FlutterMethodCall*)call result:(FlutterResult)result { + JPushNotificationContent *content = [[JPushNotificationContent alloc] init]; + NSDictionary *params = call.arguments; + if (params[@"title"]) { + content.title = params[@"title"]; + } + + if (![params[@"subtitle"] isEqualToString:@""]) { + content.subtitle = params[@"subtitle"]; + } + + if (params[@"content"]) { + content.body = params[@"content"]; + } + + if (params[@"badge"]) { + content.badge = params[@"badge"]; + } + + if (![params[@"action"] isEqualToString:@""]) { + content.action = params[@"action"]; + } + + if (![params[@"extra"] isEqualToString:@""]) { + content.userInfo = params[@"extra"]; + } + + if (![params[@"sound"] isEqualToString:@""]) { + content.sound = params[@"sound"]; + } + + JPushNotificationTrigger *trigger = [[JPushNotificationTrigger alloc] init]; + if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) { + if (params[@"fireTime"]) { + NSNumber *date = params[@"fireTime"]; + NSTimeInterval currentInterval = [[NSDate date] timeIntervalSince1970]; + NSTimeInterval interval = [date doubleValue]/1000 - currentInterval; + interval = interval>0?interval:0; + trigger.timeInterval = interval; + } + } + + else { + if (params[@"fireTime"]) { + NSNumber *date = params[@"fireTime"]; + trigger.fireDate = [NSDate dateWithTimeIntervalSince1970: [date doubleValue]/1000]; + } + } + JPushNotificationRequest *request = [[JPushNotificationRequest alloc] init]; + request.content = content; + request.trigger = trigger; + + if (params[@"id"]) { + NSNumber *identify = params[@"id"]; + request.requestIdentifier = [identify stringValue]; + } + request.completionHandler = ^(id result) { + NSLog(@"result"); + }; + + [JPUSHService addNotification:request]; + + result(@[@[]]); +} + - (void)dealloc { _isJPushDidLogin = NO; [[NSNotificationCenter defaultCenter] removeObserver:self]; diff --git a/lib/jpush_flutter.dart b/lib/jpush_flutter.dart index 17b1089..a3adf8b 100644 --- a/lib/jpush_flutter.dart +++ b/lib/jpush_flutter.dart @@ -200,6 +200,16 @@ class JPush { final String rid = await _channel.invokeMethod('getRegistrationID'); return rid; } + + /// + /// 发送本地通知到调度器,指定时间出发该通知。 + /// @param {Notification} notification + /// + static Future sendLocalNotification(LocalNotification notification) async { + await _channel.invokeMethod('sendLocalNotification', notification.toMap()); + + return notification.toMap().toString(); + } } class NotificationSettingsIOS { @@ -218,3 +228,63 @@ class NotificationSettingsIOS { } } + + + + + + /// @property {number} [buildId] - 通知样式:1 为基础样式,2 为自定义样式(需先调用 `setStyleCustom` 设置自定义样式) + /// @property {number} [id] - 通知 id, 可用于取消通知 + /// @property {string} [title] - 通知标题 + /// @property {string} [content] - 通知内容 + /// @property {object} [extra] - extra 字段 + /// @property {number} [fireTime] - 通知触发时间(毫秒) + /// // iOS Only + /// @property {number} [badge] - 本地推送触发后应用角标值 + /// // iOS Only + /// @property {string} [soundName] - 指定推送的音频文件 + /// // iOS 10+ Only + /// @property {string} [subtitle] - 子标题 +class LocalNotification { + + final int buildId;//? + final int id; + final String title; + final String content; + final Map extras;//? + final DateTime fireTime; + final int badge;//? + final String soundName;//? + final String subtitle;//? + + const LocalNotification ({ + @required this.id, + @required this.title, + @required this.content, + @required this.fireTime, + this.buildId, + this.extras, + this.badge = 0, + this.soundName, + this.subtitle + }): + assert(id != null), + assert(title != null), + assert(content != null), + assert(fireTime != null); + + Map toMap() { + return { + 'id': id, + 'title': title, + 'content': content, + 'fireTime': fireTime.millisecondsSinceEpoch, + 'buildId': buildId, + 'extras': extras, + 'badge': badge, + 'soundName': soundName, + 'subtitle': subtitle + }; + } + +}