预定的本地通知不会突然出现
Scheduled local notifications are not coming in flutter
我有一个应用会在特定时间显示通知,比如每天上午 10 点。我使用 flutter_local_notifications: ^1.4.1
进行通知。在 GitHub 页面上,我创建了通知并实时收到通知,当我激活计划通知按钮时说从现在开始 10 秒后,我没有收到回复。在 android.
的模拟器中试过
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() => runApp(AwesomeTime());
class AwesomeTime extends StatefulWidget {
@override
_AwesomeTimeState createState() => _AwesomeTimeState();
}
class _AwesomeTimeState extends State<AwesomeTime> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
void initState() {
super.initState();
initNotification();
}
initNotification() {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
FlutterLocalNotificationsPlugin().initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
Future<void> onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
}
showNotification() async {
var scheduledNotificationDateTime =
DateTime.now().add(Duration(seconds: 5));
var android = new AndroidNotificationDetails(
'Channel ID',
'Channel Name',
'channelDescription',
importance: Importance.Max,
priority: Priority.High,
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
// await flutterLocalNotificationsPlugin.show(
// 0, 'current reminder', 'current reminder', platform,
// payload: 'Awesome');
await flutterLocalNotificationsPlugin.schedule(0, 'Schedule Reminder',
'Schedule reminder', scheduledNotificationDateTime, platform);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: Text('push notifications'),
onPressed: () {
showNotification();
},
),
),
),
);
}
}
在文档中,他们提到需要对我在 android/app/src/main/AndroidManifest.xml
中更新的 AndroidManifest.xml
进行一些更新
以下是变化
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name="com.example.first_app.ScheduledNotificationReceiver" />
<receiver android:name="com.example.first_app.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<service android:name="com.example.first_app.localnotifications.services.LocalNotificationsService"
请阅读 flutter_local_notifications
carefully.In 中的文档以便安排通知工作,您必须添加以下行。解决方案是这样的:在 AndroidManifest.xml
之后添加这些行
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
我有一个应用会在特定时间显示通知,比如每天上午 10 点。我使用 flutter_local_notifications: ^1.4.1
进行通知。在 GitHub 页面上,我创建了通知并实时收到通知,当我激活计划通知按钮时说从现在开始 10 秒后,我没有收到回复。在 android.
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() => runApp(AwesomeTime());
class AwesomeTime extends StatefulWidget {
@override
_AwesomeTimeState createState() => _AwesomeTimeState();
}
class _AwesomeTimeState extends State<AwesomeTime> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
void initState() {
super.initState();
initNotification();
}
initNotification() {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
FlutterLocalNotificationsPlugin().initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
Future<void> onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
}
showNotification() async {
var scheduledNotificationDateTime =
DateTime.now().add(Duration(seconds: 5));
var android = new AndroidNotificationDetails(
'Channel ID',
'Channel Name',
'channelDescription',
importance: Importance.Max,
priority: Priority.High,
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
// await flutterLocalNotificationsPlugin.show(
// 0, 'current reminder', 'current reminder', platform,
// payload: 'Awesome');
await flutterLocalNotificationsPlugin.schedule(0, 'Schedule Reminder',
'Schedule reminder', scheduledNotificationDateTime, platform);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: Text('push notifications'),
onPressed: () {
showNotification();
},
),
),
),
);
}
}
在文档中,他们提到需要对我在 android/app/src/main/AndroidManifest.xml
中更新的 AndroidManifest.xml
进行一些更新
以下是变化
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name="com.example.first_app.ScheduledNotificationReceiver" />
<receiver android:name="com.example.first_app.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<service android:name="com.example.first_app.localnotifications.services.LocalNotificationsService"
请阅读 flutter_local_notifications
carefully.In 中的文档以便安排通知工作,您必须添加以下行。解决方案是这样的:在 AndroidManifest.xml
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />