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