如何使用flutter_local_notifications

时间:2019-12-29 18:48:52

标签: flutter dart

使用createScheduledNotification()和showNotification()可以但使用 createDailyNotification()给出以下错误。

Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 5 of 558 libraries in 333ms.
   E/MethodChannel#dexterous.com/flutter/local_notifications( 6909): Failed to handle method call
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909): java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.repeatNotification(FlutterLocalNotificationsPlugin.java:305)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.repeat(FlutterLocalNotificationsPlugin.java:743)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:700)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at android.os.MessageQueue.nativePollOnce(Native Method)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at android.os.MessageQueue.next(MessageQueue.java:336)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at android.os.Looper.loop(Looper.java:174)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at android.app.ActivityThread.main(ActivityThread.java:7356)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at java.lang.reflect.Method.invoke(Native Method)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    E/MethodChannel#dexterous.com/flutter/local_notifications( 6909):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
    I/flutter ( 6909): PlatformException(error, Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference, null)
    I/flutter ( 6909): Instance of 'Time'Instance of 'AndroidNotificationDetails'Instance of 'IOSNotificationDetails'Instance of 'NotificationDetails'
    I/flutter ( 6909): notification payload: 

请参见下面的代码,运行flutter doctor -v不会出错!**

    import 'dart:async';
    import 'dart:ui';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
    class NotificationHomePage extends StatefulWidget {
      @override
      _NotificationHomePageState createState() => _NotificationHomePageState();
    }
    class _NotificationHomePageState extends State<NotificationHomePage> {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();
      var initializationSettingsAndroid;
      var initializationSettingsIOS;
      var initializationSettings;
      var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          'repeatDailyAtTime_channel_id_ielts',
          'repeatDailyAtTime_channel_name_ielts',
          'repeatDailyAtTime_description_ielts');
      var iOSPlatformChannelSpecifics = IOSNotificationDetails();
      var platformChannelSpecifics;
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
        initializationSettingsIOS = IOSInitializationSettings(
            onDidReceiveLocalNotification: onDidReceiveLocalNotification);
        initializationSettings = InitializationSettings(
            initializationSettingsAndroid, initializationSettingsIOS);
        platformChannelSpecifics = NotificationDetails(
            androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        flutterLocalNotificationsPlugin
            .initialize(
          initializationSettings,
          onSelectNotification: onSelectNotification,
        )
            .then((init) {
          setupNotification();
        });
      }
      Future onSelectNotification(String payload) async {
        if (payload != null) {
          debugPrint('notification payload: ' + payload);
        } else {
          print('null payload');
        }
      }
      Future onDidReceiveLocalNotification(
          int id, String title, String body, String payload) async {
        // display a dialog with the notification details, tap ok to go to another page
        showDialog(
            context: context,
            builder: (context) => AlertDialog(
                  content: Text('dont forget'),
                  actions: [
                    FlatButton(
                      child: Text('Ok'),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                ));
      }
      String _toTwoDigitString(int value) {
        return value.toString().padLeft(2, '0');
      }
      Future<void> createNotifications() async {
        var time = Time(20, 15, 10);
        var time2 = Time(21, 00, 10);
        await flutterLocalNotificationsPlugin.showDailyAtTime(
            0, 'IELTS ', 'Time to learn', time, platformChannelSpecifics);
        await flutterLocalNotificationsPlugin.showDailyAtTime(
            1, 'IELTS', 'Time to learn', time2, platformChannelSpecifics);
      }
      Future<void> createDailyNotification() async {
        var time = new Time(01, 08, 00);
        try {
          await flutterLocalNotificationsPlugin.showDailyAtTime(
              1,
              'show daily title',
              'Daily notification shown at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
              time,
              platformChannelSpecifics);
        } catch (e) {
          print(e.toString());
        }
        print(time.toString() +
            androidPlatformChannelSpecifics.toString() +
            iOSPlatformChannelSpecifics.toString() +
            platformChannelSpecifics.toString());
      }
      Future<void> createWeeklyNotification() async {
        var time = new Time(10, 0, 0);
        await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
            0,
            'show weekly title',
            'Weekly notification shown on Monday at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
            Day.Monday,
            time,
            platformChannelSpecifics);
      }
      Future<void> createScheduledNotification() async {
        var scheduledNotificationDateTime =
            new DateTime.now().add(new Duration(seconds: 5));
        var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
            'your other channel id',
            'your other channel name',
            'your other channel description');
        var iOSPlatformChannelSpecifics = new IOSNotificationDetails(
            presentAlert: true, presentBadge: true, presentSound: true);
        NotificationDetails platformChannelSpecifics = new NotificationDetails(
            androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.schedule(
            0,
            'Minerva Time',
            'Minerva Time!!!',
            scheduledNotificationDateTime,
            platformChannelSpecifics);
      }
      Future<void> createPerNotification() async {
        // Show a notification every minute with the first appearance happening a minute after invoking the method
        await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title',
            'repeating body', RepeatInterval.EveryMinute, platformChannelSpecifics);
      }
      showNotification() async {
        await flutterLocalNotificationsPlugin.show(
            0, 'plain title', 'plain body', platformChannelSpecifics,
            payload: 'item x');
      }
      Future<void> setupNotification() async {
        try {
          await createScheduledNotification();
          //await createDailyNotification();
          //await showNotification();
          //await createNotifications();
        } catch (e) {
          print(e.toString());
        }
        createDailyNotification();
        //createWeeklyNotification();
        createScheduledNotification();
      }
      cancellAllNotifications() async {
        await flutterLocalNotificationsPlugin.cancelAll();
      }
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Text(
                  'hello world notifi',
                  style: TextStyle(fontSize: 25),
                ),
                FlatButton(
                  child: Text('press'),
                  onPressed: () {
                    cancellAllNotifications();
                  },
                )
              ],
            ),
          ),
        );
      }
    }

0 个答案:

没有答案