【问题标题】:Scheduled local notifications are not coming in flutter预定的本地通知不会出现
【发布时间】:2020-04-14 10:07:00
【问题描述】:

我有一个应用程序会在特定时间显示通知,比如每天上午 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();
            },
          ),
        ),
      ),
    );
  }
}

在文档中,他们提到需要对 AndroidManifest.xml 进行一些更新,我已在 android/app/src/main/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"

【问题讨论】:

    标签: android flutter dart push-notification notifications


    【解决方案1】:

    请仔细阅读flutter_local_notifications 中的文档。为了安排通知工作,您必须添加以下行。解决方案是这样的:在 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" />
    

    【讨论】:

    • 在AdnroidManifest.xml中的之后添加sn-p代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多