Commit 46d70639 authored by xgz's avatar xgz

增加ios推送

parent 20412e03
import 'package:flutter/material.dart'; // import 'package:flutter/material.dart';
import 'dart:async'; // import 'dart:async';
import 'package:flutter/services.dart'; // import 'package:flutter/services.dart';
import 'package:flutter_jpush_vip/flutter_jpush_vip.dart'; // import 'package:flutter_jpush_vip/flutter_jpush_vip.dart';
void main() { // void main() {
runApp(MyApp()); // runApp(MyApp());
} // }
class MyApp extends StatefulWidget { // class MyApp extends StatefulWidget {
@override // @override
_MyAppState createState() => _MyAppState(); // _MyAppState createState() => _MyAppState();
} // }
class _MyAppState extends State<MyApp> { // class _MyAppState extends State<MyApp> {
@override // @override
void initState() { // void initState() {
super.initState(); // super.initState();
FlutterJpushVip.init().then((value) { // FlutterJpushVip.init().then((value) {
print("============$value"); // print("============$value");
}); // });
FlutterJpushVip.onNotification((n){ // FlutterJpushVip.onNotification((n){
print("============$n"); // print("============$n");
}); // });
} // }
// @override
@override // Widget build(BuildContext context) {
Widget build(BuildContext context) { // return MaterialApp(
return MaterialApp( // home: Scaffold(
home: Scaffold( // appBar: AppBar(
appBar: AppBar( // title: const Text('Plugin example app'),
title: const Text('Plugin example app'), // ),
), // body: GestureDetector(
body: GestureDetector( // onTap: () {
onTap: () {
// },
}, // child: Center(
child: Center( // child: Text('Running on: test'),
child: Text('Running on: test'), // )),
)), // ),
), // );
); // }
} // }
}
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_jpush_vip_example/main.dart';
void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data.startsWith('Running on:'),
),
findsOneWidget,
);
});
}
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>
@interface FlutterJpushVipPlugin : NSObject<FlutterPlugin> @interface FlutterJpushVipPlugin : NSObject<FlutterPlugin>
@property FlutterMethodChannel *channel;
+ (void)appdidLaunch:(NSDictionary*)launchOption;
@end @end
This diff is collapsed.
import Flutter
import UIKit
public class SwiftFlutterJpushVipPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_jpush_vip", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterJpushVipPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}
...@@ -16,6 +16,11 @@ Pod::Spec.new do |s| ...@@ -16,6 +16,11 @@ Pod::Spec.new do |s|
s.source_files = 'Classes/**/*' s.source_files = 'Classes/**/*'
s.dependency 'Flutter' s.dependency 'Flutter'
s.platform = :ios, '8.0' s.platform = :ios, '8.0'
s.dependency 'JCore','2.4.0'
s.dependency 'JPush','3.4.0'
s.ios.deployment_target = '10.0'
s.static_framework = true
# Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported. # Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' } s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' }
......
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class FlutterJpushVip { class FlutterJpushVip {
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('flutter_jpush_vip'); const MethodChannel('flutter_jpush_vip');
/// 初始化 /// 初始化
static Future<_InitResult> init() async { /// [production] ios only 生产环境
var map = await _channel.invokeMethod('init'); static Future<_InitResult> init({bool production = true}) async {
var map = await _channel.invokeMethod('init', {'production': production});
return _InitResult.from(map); return _InitResult.from(map);
} }
///
/// iOS Only
/// 申请推送权限,注意这个方法只会向用户弹出一次推送权限请求(如果用户不同意,之后只能用户到设置页面里面勾选相应权限),需要开发者选择合适的时机调用。
///
void applyPushAuthority(
[NotificationSettingsIOS iosSettings = const NotificationSettingsIOS()]) {
if (!Platform.isIOS) {
return;
}
_channel.invokeMethod('applyPushAuthority', iosSettings.toMap());
}
/// 开启调试模式 /// 开启调试模式
static void debug() { static void debug() {
_channel.invokeMethod('debug'); _channel.invokeMethod('debug');
} }
/// 获取注册ID /// 获取注册ID
static Future<String> getRegistrationID() { static Future<String> getRegistrationID() {
return _channel.invokeMethod('getRegistrationID'); return _channel.invokeMethod('getRegistrationID');
} }
/// 清除通知 /// 清除通知
static void clearAllNotifications() { static void clearAllNotifications() {
_channel.invokeMethod('clearAllNotifications'); _channel.invokeMethod('clearAllNotifications');
} }
/// 监听通知 /// 监听通知
static void onNotification(Function(_Notification notification) callback) { static void onNotification(Function(_Notification notification) callback) {
_channel.setMethodCallHandler((call) { _channel.setMethodCallHandler((call) {
if (call.method == '__JPUSH_MESSAGE__') { if (call.method == '__JPUSH_MESSAGE__') {
try { try {
Map map = json.decode(call.arguments); Map map = call.arguments;
if (call.arguments.runtimeType.toString().contains('String')) {
map = json.decode(call.arguments);
}
var n = _Notification.from(map); var n = _Notification.from(map);
callback(n); callback(n);
} catch (e) { } catch (e) {
...@@ -70,3 +92,19 @@ class _Notification { ...@@ -70,3 +92,19 @@ class _Notification {
return 'Notification{msgId: $msgId, content: $content, title: $title, extras: $extras, platform: $platform}'; return 'Notification{msgId: $msgId, content: $content, title: $title, extras: $extras, platform: $platform}';
} }
} }
class NotificationSettingsIOS {
final bool sound;
final bool alert;
final bool badge;
const NotificationSettingsIOS({
this.sound = true,
this.alert = true,
this.badge = true,
});
Map<String, dynamic> toMap() {
return <String, bool>{'sound': sound, 'alert': alert, 'badge': badge};
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment