我flutter_local_notifications在我的應用程式中使用依賴項在按下按鈕后發送本地通知。在應用程式中,執行按鈕時,應該會出現本地通知,并且應用程式應該導航到第二個螢屏。但是,只有第二個操作正在執行,并且沒有顯示通知。我參考了這個 YouTube 教程,在我的應用程式中創建本地通知。
notificationservice.dart我的應用程式的檔案是:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
class NotificationService {
static final NotificationService _notificationService = NotificationService._internal();
factory NotificationService() {
return _notificationService;
}
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
NotificationService._internal();
Future<void> initNotification() async {
final AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@drawable/ic_launcher');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
);
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
Future<void> showNotification(int id, String title, String body, int seconds) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
const NotificationDetails(
android: AndroidNotificationDetails(
'main_channel',
'Main Channel',
channelDescription: 'Main channel notifications',
importance: Importance.max,
priority: Priority.max,
icon: '@drawable/ic_launcher'
),
iOS: IOSNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
}
TextButtonwidget中本地通知功能的實作如下:
TextButton(
onPressed: () {
//lines of codes
NotificationService().showNotification(1, "title", "body", 5);
Navigator.of(context).pushNamed(CaseConfirmationScreen.routeName);
},
child: Text(
"File Case",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 26.0,
fontWeight: FontWeight.w500,
letterSpacing: 2.0,
),
),
style: TextButton.styleFrom(
backgroundColor: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
padding: const EdgeInsets.symmetric(
horizontal: 22.0,
vertical: 15.0,
),
),
),
除錯控制臺中顯示的錯誤是這樣的。
Youtube教程中的應用程式源代碼
我應該怎么做才能清除這個錯誤?
uj5u.com熱心網友回復:
將您的圖示添加到 [projectFolder]/android/app/src/main/res/drawable(例如 app_icon.png)并在此處使用該名稱:
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375659.html
上一篇:不執行顫振權限處理程式
