我正在使用flutter_local_notifications包在应用程序内外显示通知(后台模式)。在Android(调试+发布)和iOS调试模式下,一切都按预期运行,但奇怪的是,在iOS发布的后台模式下,本地通知不显示。
我不知道为什么iOS的发布模式表现不同,有什么想法吗?
谢谢.
我已经检查过了:
- 使用最新版本的flutter_local_notifications软件包(v16.1.0)
- 检查调试和发布模式的“签名和功能”(后台模式:获取、远程通知)
我的通知类别:
import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class ReceivedNotification {
ReceivedNotification({
required this.id,
required this.title,
required this.body,
required this.payload,
});
final int id;
final String? title;
final String? body;
final String? payload;
}
/// A notification action which triggers a App navigation event
const String navigationActionId = 'id_3';
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
// ignore: avoid_print
print('notification(${notificationResponse.id}) action tapped: '
'${notificationResponse.actionId} with'
' payload: ${notificationResponse.payload}');
if (notificationResponse.input?.isNotEmpty ?? false) {
// ignore: avoid_print
print(
'notification action tapped with input: ${notificationResponse.input}');
}
}
class NotificationService {
//Singleton pattern
static final NotificationService _notificationService =
NotificationService._internal();
factory NotificationService() {
return _notificationService;
}
NotificationService._internal();
/// Streams are created so that app can respond to notification-related events
/// since the plugin is initialised in the `main` function
final StreamController<ReceivedNotification>
didReceiveLocalNotificationStream =
StreamController<ReceivedNotification>.broadcast();
final StreamController<String?> selectNotificationStream =
StreamController<String?>.broadcast();
//instance of FlutterLocalNotificationsPlugin
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initialize() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@drawable/iclauncher');
//Initialization Settings for iOS devices
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
requestSoundPermission: true,
requestBadgePermission: true,
requestAlertPermission: true,
onDidReceiveLocalNotification:
(int id, String? title, String? body, String? payload) async {
didReceiveLocalNotificationStream.add(
ReceivedNotification(
id: id,
title: title,
body: body,
payload: payload,
),
);
},
);
final InitializationSettings settings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
);
await flutterLocalNotificationsPlugin.initialize(
settings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) {
switch (notificationResponse.notificationResponseType) {
case NotificationResponseType.selectedNotification:
selectNotificationStream.add(notificationResponse.payload);
break;
case NotificationResponseType.selectedNotificationAction:
if (notificationResponse.actionId == navigationActionId) {
selectNotificationStream.add(notificationResponse.payload);
}
break;
}
},
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
}
Future<NotificationDetails> _notificationDetails() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'channel ID',
'channel name',
playSound: true,
priority: Priority.high,
importance: Importance.high,
);
const NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
return notificationDetails;
}
Future<void> showNotification({
required int id,
required String title,
required String body,
}) async {
final details = await _notificationDetails();
await flutterLocalNotificationsPlugin.show(id, title, body, details);
}
}
字符串
1条答案
按热度按时间5fjcxozz1#
我认为你的iOS初始化问题造成了你的问题。我正在使用这个类...所以请检查这个并更新你的代码。最重要的是检查iOS初始化部分。
字符串