-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlocal_notification.dart
109 lines (93 loc) · 3.59 KB
/
local_notification.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotificationUtil {
static LocalNotificationUtil _instance;
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;
NotificationAppLaunchDetails _notificationAppLaunchDetails;
FlutterLocalNotificationsPlugin get flutterLocalNotificationsPlugin =>
_flutterLocalNotificationsPlugin;
NotificationAppLaunchDetails get notificationAppLaunchDetails =>
_notificationAppLaunchDetails;
LocalNotificationUtil._();
/// 必须先调用
initialization() async {
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
// notification
_notificationAppLaunchDetails = await _flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails();
var initializationSettingsAndroid =
AndroidInitializationSettings('ic_launcher');
// Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
// of the `IOSFlutterLocalNotificationsPlugin` class
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: false, //初始化不请求权限
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification: null);
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: new MacOSInitializationSettings());
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {}
});
}
/// 需要先请求权限
requestPermissions() {
/// iOS获取权限
if (Platform.isIOS) {
_flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
if (Platform.isAndroid) {
_flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(AndroidNotificationChannel(
AndroidChannelId.upload_channel, '通知提示', '上传通知提示'));
}
}
/// 显示
show(int id, String title, String body,
NotificationDetails notificationDetails) async {
await _flutterLocalNotificationsPlugin.show(
id, title, body, notificationDetails);
}
/// 实例获取
static LocalNotificationUtil getInstance() {
if (_instance == null) {
_instance = LocalNotificationUtil._();
}
return _instance;
}
/// 上传Channel
static AndroidNotificationDetails uploadAndroidChannel() {
return AndroidNotificationDetails(
AndroidChannelId.upload_channel, '上传通知', '上传通知提示',
importance: Importance.max, priority: Priority.high, ticker: 'ticker');
}
/// 默认IOS
static IOSNotificationDetails normalIOSNotificationDetails() {
return IOSNotificationDetails();
}
/// 默认MacOS
static MacOSNotificationDetails normalMacOSNotificationDetails() {
return MacOSNotificationDetails();
}
static NotificationDetails createNotificationDetails(
AndroidNotificationDetails android,
IOSNotificationDetails iOS,
MacOSNotificationDetails macOS) {
return NotificationDetails(android: android, iOS: iOS, macOS: macOS);
}
}
class AndroidChannelId {
static const upload_channel = '10000';
}