Flutter 本地通知无法在后台运行?

Zil*_*aid 13 localnotification dart flutter flutter-notification

我正在使用 flutter 本地通知在我的应用程序中显示日程通知。但不幸的是,当应用程序处于终止状态时它不起作用。

这是我的代码:

class Notifications {
static final FlutterLocalNotificationsPlugin _notifications =
  FlutterLocalNotificationsPlugin();

static Future<NotificationDetails> _notificationDetails() async {
return const NotificationDetails(
    android: AndroidNotificationDetails(
      'weekly notification channel id',
      'weekly notification channel name',
      channelDescription: 'weekly notification description',
      playSound: true,
      sound: RawResourceAndroidNotificationSound('azan1'),
      importance: Importance.high,
    ),
    iOS: IOSNotificationDetails(sound: 'azan1.mp3', presentSound: true));
}

static void init() async {
tz.initializeTimeZones();
const AndroidInitializationSettings android =
    AndroidInitializationSettings('@mipmap/ic_launcher');
const IOSInitializationSettings iOS = IOSInitializationSettings();
InitializationSettings settings =
    const InitializationSettings(android: android, iOS: iOS);
await _notifications.initialize(settings);
final String locationName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(locationName));
}

static void showScheduledNotification(
int id, {
required DateTime scheduledDate,
String? title,
String? body,
String? payload,
}) async {
await _notifications.zonedSchedule(
  id,
  'Azan Time',
  '$body Prayer Time',
  _scheduleDaily(
      Time(scheduledDate.hour, scheduledDate.minute, scheduledDate.second)),
  await _notificationDetails(),
  androidAllowWhileIdle: true,
  uiLocalNotificationDateInterpretation:
      UILocalNotificationDateInterpretation.absoluteTime,
  matchDateTimeComponents: DateTimeComponents.time,
  payload: payload,
 );
 }

static tz.TZDateTime _scheduleDaily(Time time) {
tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime schdeuledDate = tz.TZDateTime(tz.local, now.year, now.month,
    now.day, time.hour, time.minute, time.second);
return schdeuledDate.isBefore(now)
    ? schdeuledDate.add(const Duration(days:1))
    : schdeuledDate;
}

static Future<void> cancelNotification(int id) async {
await _notifications.cancel(id);
}

static Future<void> cancelAllNotifications() async {
await _notifications.cancelAll();
 }
 }
Run Code Online (Sandbox Code Playgroud)

我还在Android.xml文件中添加了所有属性。但它仍然不起作用,如果有人知道这个问题的解决方案请回答这个问题。

Tas*_*hin 0

在您的 Application.java 中,如果这部分不起作用,您可以转义它来运行应用程序。

\n
private void createNotificationChannels() {\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\n            NotificationChannel channelHigh = new NotificationChannel(\n                    CHANNEL_HIGH,\n                    "Notificaci\xc3\xb3n Importante",\n                    NotificationManager.IMPORTANCE_HIGH\n            );\n\n            channelHigh.setDescription("Canal Alto");\n\n            NotificationManager manager = getSystemService(NotificationManager.class);\n            manager.createNotificationChannel(channelHigh);\n\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n
\n

检查清单文件,不要忘记添加

\n
  <meta-data\n    android:name="com.google.firebase.messaging.default_notification_channel_id"\n    android:value="default"/>\n
Run Code Online (Sandbox Code Playgroud)\n

欲了解更多请查看此链接

\n