简体   繁体   English

Flutter 本地通知计划 - 未按预期工作

[英]Flutter local notification schedule - not working as expected

I want to show notification daily on a specified time and I have implemented the code to show daily notification but it is not working not showing a notification at the time I want to call.我想在指定的时间每天显示通知,并且我已经实现了显示每日通知的代码,但是在我想打电话时没有显示通知是行不通的。 I am using this plugin link我正在使用这个插件链接

When I run The App I got this warning on Console here is the screenshot当我运行应用程序时,我在控制台上收到此警告,这是屏幕截图一只忙碌的猫

Is my implementation of the code wrong?我的代码实现是错误的吗? Here is the code这是代码

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:async';

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}


class _AppState extends State<App> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

  showNotification() async{
    var time = new Time(3,51, 0);//at 3.30 
    var androidPlatformChannelSpecifics =
    new AndroidNotificationDetails('repeatDailyAtTime channel id',
        'repeatDailyAtTime channel name', 'repeatDailyAtTime description');
    var iOSPlatformChannelSpecifics =
    new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.showDailyAtTime(
        0,
        'Teer Result Time',
        'Open The App and check for the Result',
        time,
        platformChannelSpecifics);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    showNotification();
  }
  @override
  Widget build(BuildContext context) {
    return Container(

    );
  }
}

You have to first initialize Local Notification plugin.您必须首先初始化本地通知插件。

Just add the initialization code to your initstate, as shown below:只需将初始化代码添加到您的 initstate 中,如下所示:

    import 'package:flutter/material.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'dart:async';

    class App extends StatefulWidget {
      @override
      _AppState createState() => _AppState();
    }


    class _AppState extends State<App> {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

      showNotification() async{
        var time = new Time(3,51, 0);//at 3.30 
        var androidPlatformChannelSpecifics =
        new AndroidNotificationDetails('repeatDailyAtTime channel id',
            'repeatDailyAtTime channel name', 'repeatDailyAtTime description');
        var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails();
        var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.showDailyAtTime(
            0,
            'Teer Result Time',
            'Open The App and check for the Result',
            time,
            platformChannelSpecifics);
      }

      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        //-------------------- Initialization Code---------------------
        FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
   //--------------------------------------
        showNotification();
      }
      @override
      Widget build(BuildContext context) {
        return Container(

        );
      }
    }

I just had the same problem, I found that I didn't add some required permissions and receivers in my manifest, all the details are written in the docs under the Scheduled notifications title, as well as this link to the example project manifest as a reference.我刚刚遇到了同样的问题,我发现我没有在清单中添加一些必需的权限和接收器,所有详细信息都写在计划通知标题下的文档中,以及指向示例项目清单的链接作为参考。

Hope it helps someone.希望它可以帮助某人。

I've heard of one report where this issue happened as their app hadn't been migrated to AndroidX, although the error wouldn't imply that.我听说过一份报告,其中发生了此问题,因为他们的应用程序尚未迁移到 AndroidX,尽管该错误并不暗示这一点。 Have you looked at that yet?你看过那个了吗? See https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility#how-to-migrate-a-flutter-app-to-androidx请参阅https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility#how-to-migrate-a-flutter-app-to-androidx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM