0

我已经尝试了下面的代码,看来我还需要做一些事情。我正在使用flutter_local_notification。我需要包括firebase吗?这是我的代码

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

class NotificationApi {
static final _notifications = FlutterLocalNotificationsPlugin();
static Future _notificationDetails() async {
return NotificationDetails(
  android: AndroidNotificationDetails(
    'com.example.gym',
    'goal_channel',
    importance: Importance.max,
  ),
  iOS: IOSNotificationDetails(),
);
}

static Future showNotification({
  int id = 0,
  String? title,
  body,
  payload,
 }) async =>
  _notifications.show(
    id,
    title,
    body,
    await _notificationDetails(),
    payload: payload,
  );
}

这是结果 这是结果

4

1 回答 1

0

感谢@Amroun

这是一个工作代码

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

class NotificationApi {
 static final _notifications = FlutterLocalNotificationsPlugin();
 static Future _notificationDetails() async {
 return NotificationDetails(
  android: AndroidNotificationDetails(
  'com.example.gym',
  'goal_channel',
  icon: '@mipmap/ic_launcher',
  importance: Importance.max,
),
iOS: IOSNotificationDetails(),
);
}

 static Future showNotification({
  int id = 0,
  String? title,
  body,
  payload,
 }) async =>
_notifications.show(
id,
title,
body,
await _notificationDetails(),
payload: payload,
);
}
于 2022-01-17T13:28:12.367 回答