add localnotification api

master
huangminlinux 6 years ago
parent 2272239ea0
commit a545e7881f

@ -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/

@ -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<String, Object> 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<String, Object> extra = (HashMap<String, Object>)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();
}
}

@ -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

@ -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;

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>

@ -25,9 +25,9 @@ class _MyAppState extends State<MyApp> {
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(
@ -45,13 +45,15 @@ class _MyAppState extends State<MyApp> {
JPush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
// print("flutter onReceiveNotification: $message");
setState(() {
_platformVersion = "setAlias error: $message";
});
// setState(() {
// _platformVersion = "flutter onReceiveNotification: $message";
// });
},
onOpenNotification: (Map<String, dynamic> message) async {
print("flutter onOpenNotification: $message");
setState(() {
_platformVersion = "flutter onOpenNotification: $message";
});
},
onReceiveMessage: (Map<String, dynamic> message) async {
print("flutter onReceiveMessage: $message");
@ -90,11 +92,42 @@ class _MyAppState extends State<MyApp> {
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'),

@ -136,7 +136,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.0.2"
js:
dependency: transitive
description:

@ -143,9 +143,9 @@ static NSMutableArray<FlutterResult>* 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<FlutterResult>* getRidResults;
result(@"");
#elif TARGET_OS_IPHONE//真机
if ([JPUSHService registrationID] != nil && ![[JPUSHService registrationID] isEqualToString:@""]) {
// 如果已经成功获取 registrationID,从本地获取直接缓存
result([JPUSHService registrationID]);
@ -317,6 +318,71 @@ static NSMutableArray<FlutterResult>* 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:@"<null>"]) {
content.subtitle = params[@"subtitle"];
}
if (params[@"content"]) {
content.body = params[@"content"];
}
if (params[@"badge"]) {
content.badge = params[@"badge"];
}
if (![params[@"action"] isEqualToString:@"<null>"]) {
content.action = params[@"action"];
}
if (![params[@"extra"] isEqualToString:@"<null>"]) {
content.userInfo = params[@"extra"];
}
if (![params[@"sound"] isEqualToString:@"<null>"]) {
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];

@ -200,6 +200,16 @@ class JPush {
final String rid = await _channel.invokeMethod('getRegistrationID');
return rid;
}
///
///
/// @param {Notification} notification
///
static Future<String> 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<String, dynamic> 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<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'title': title,
'content': content,
'fireTime': fireTime.millisecondsSinceEpoch,
'buildId': buildId,
'extras': extras,
'badge': badge,
'soundName': soundName,
'subtitle': subtitle
};
}
}

Loading…
Cancel
Save