1

我正在使用 Flutter 构建一个 Android 应用程序。我的应用需要显示本地通知。我正在使用这个包,https://pub.dev/packages/flutter_local_notifications。当我在模拟器上显示通知时,它正在工作。但它不起作用,当我编译发布 APK 并将其安装在实际设备上时。

这是 NotificationService 类

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationService
{
  static FlutterLocalNotificationsPlugin? flip;

  static FlutterLocalNotificationsPlugin getFlipInstance()
  {
    if (flip == null) {
      flip = FlutterLocalNotificationsPlugin();

      // app_icon needs to be a added as a drawable
      // resource to the Android head project.
      var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
      var ios = const IOSInitializationSettings();

      var settings = InitializationSettings(android: android, iOS: ios);
      flip!.initialize(settings);
    }

    return flip as FlutterLocalNotificationsPlugin;
  }


  static Future showNotification(String title, String body) async {
    var androidPlatformChannelSpecifics = const AndroidNotificationDetails("your channel id", "my channel name");
    var iOSPlatformChannelSpecifics = const IOSNotificationDetails();

    // initialise channel platform for both Android and iOS device.
    var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);

    await getFlipInstance().show(0, title,
        body,
        platformChannelSpecifics, payload: 'Default_Sound'
    );
  }
}

我在主页的 initState 方法中显示这样的通知。

@override
  void initState() {
    super.initState();
    setState(() {
    NotificationService.showNotification("Local Notification", "Local Notification Message");
  }

正如我所提到的,它适用于模拟器。但是当我生成一个 APK 并将其安装在真实设备上时,它无法正常工作。

4

0 回答 0