关闭应用程序时在 Flutter 中显示通知

时间:2021-03-23 09:20:39

标签: flutter dart

我尝试在我已经使用过的 Flutter 应用程序中显示通知 FlutterLocalNotificationPlugin

  1. 当应用程序打开时,会显示通知,但
  2. 当 Flutter 应用程序关闭但无法运行或未显示通知时,我是 Flutter 新手,请帮助我

//这是函数初始化

  FlutterLocalNotificationsPlugin localNotificationsPlugin;
  Future showNotification() async {
  var androidDetails = new AndroidNotificationDetails(
  "channelId",
  "channelName",
  "channelDescription",
  importance: Importance.max,
  priority: Priority.high,
  playSound: true,
  largeIcon: DrawableResourceAndroidBitmap('tele_marketing'),
);
 var iosDetails = new IOSNotificationDetails(
    //sound: 'bells.mp3',
    presentAlert: true,
    presentBadge: true,
    presentSound: true);
    var generalNotification =
    new NotificationDetails(android: androidDetails, iOS: iosDetails);
    await localNotificationsPlugin.show(
    0, "Total Leads", totalLeads.toString(), generalNotification);
  }

// 这是函数调用

 @override
 void initState() {
 super.initState();
 //Build Notification in IOS and Android
 var androidNotification =
    new AndroidInitializationSettings('tele_marketing');
 var iosNotification = new IOSInitializationSettings(
  requestAlertPermission: true,
  requestBadgePermission: true,
  requestSoundPermission: true,
 );
 var notification = new InitializationSettings(
     android: androidNotification, iOS: iosNotification);
 localNotificationsPlugin = new FlutterLocalNotificationsPlugin();
 localNotificationsPlugin.initialize(notification);
}

//调用API时显示功能应用是打开还是关闭

                 if (snapshot.hasData) {
                    if (totalLeads > totLeads) {
                      playSound();
                      showNotification();
                      totLeads = totalLeads;
                    } else {}

0 个答案:

没有答案