好的,用颤振推送通知非常容易。将这些依赖项添加到pubspec.yaml:
dependencies:
flutter_local_notifications: ^1.4.2
rxdart: ^0.23.1
特定插件:flutter_local_notifications和rxdart
然后在终端中运行这个命令:
flutter pub get
转到AndroidManifest.xml并/android/app/src/main/
添加以下行:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
现在,在Add中转到AppDelegate.swift :/ios/Runner/
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as ?
UNUserNotificationCenterDelegate
}
在这些行之前:
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
完毕?
现在,创建一个notifications_helper.dart
文件并导入flutter_local_notifications
和rxdart packages
. 然后在该文件中添加以下内容:
class NotificationClass{
final int id;
final String title;
final String body;
final String payload;
NotificationClass({this.id, this.body, this.payload, this.title});
}
还要添加这些finals
:
final rxSub.BehaviorSubject<NotificationClass> didReceiveLocalNotificationSubject =
rxSub.BehaviorSubject<NotificationClass>();
final rxSub.BehaviorSubject<String> selectNotificationSubject =
rxSub.BehaviorSubject<String>();
现在最后将以下内容添加method
到帮助文件中:
Future<void> initNotifications(
notifs.FlutterLocalNotificationsPlugin
notifsPlugin) async {
var initializationSettingsAndroid =
notifs.AndroidInitializationSettings('icon');
var initializationSettingsIOS = notifs.IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject
.add(NotificationClass(id: id, title: title, body: body, payload: payload));
});
var initializationSettings = notifs.InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await notifsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
print('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
});
print("Notifications initialised successfully");
}
iOS的权限请求方法:
void requestIOSPermissions(
notifs.FlutterLocalNotificationsPlugin notifsPlugin) {
notifsPlugin.resolvePlatformSpecificImplementation<notifs.IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
完成了吗?
现在,预定通知,添加这些:
Future<void> scheduleNotification(
{notifs.FlutterLocalNotificationsPlugin notifsPlugin,
String id,
String title,
String body,
DateTime scheduledTime}) async {
var androidSpecifics = notifs.AndroidNotificationDetails(
id, // This specifies the ID of the Notification
'Scheduled notification', // This specifies the name of the notification channel
'A scheduled notification', //This specifies the description of the channel
icon: 'icon',
);
var iOSSpecifics = notifs.IOSNotificationDetails();
var platformChannelSpecifics = notifs.NotificationDetails(
androidSpecifics, iOSSpecifics);
await notifsPlugin.schedule(0, title, "Scheduled notification",
scheduledTime, platformChannelSpecifics); // This literally schedules the notification
}
现在修改main.dart
文件:
NotificationAppLaunchDetails notifLaunch;
final FlutterLocalNotificationsPlugin notifsPlugin=
FlutterLocalNotificationsPlugin();
现在在 main 方法中,添加
notifLaunch = await notifsPlugin.getNotificationAppLaunchDetails();
await initNotifications(notifsPlugin);
requestIOSPermissions(notifsPlugin);
现在主要的事情,触发预定的通知,导入你的helper file
和main.dart
:
import '../helpers/notifications_helper.dart';
import '../main.dart';
现在调用 scheduleNotification 方法:
scheduleNotification(
notifsPlugin: notifsPlugin, //Or whatever you've named it in main.dart
id: DateTime.now().toString(),
body: "A scheduled Notification",
scheduledTime: DateTime.now()); //Or whenever you actually want to trigger it
我的朋友,你完成了!