Flutter:本地通知在iOS发布模式下不起作用

dsf9zpds  于 2023-11-20  发布在  iOS
关注(0)|答案(1)|浏览(153)

我正在使用flutter_local_notifications包在应用程序内外显示通知(后台模式)。在Android(调试+发布)和iOS调试模式下,一切都按预期运行,但奇怪的是,在iOS发布的后台模式下,本地通知不显示。
我不知道为什么iOS的发布模式表现不同,有什么想法吗?
谢谢.
我已经检查过了:

  • 使用最新版本的flutter_local_notifications软件包(v16.1.0)
  • 检查调试和发布模式的“签名和功能”(后台模式:获取、远程通知)

我的通知类别:

  1. import 'dart:async';
  2. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  3. class ReceivedNotification {
  4. ReceivedNotification({
  5. required this.id,
  6. required this.title,
  7. required this.body,
  8. required this.payload,
  9. });
  10. final int id;
  11. final String? title;
  12. final String? body;
  13. final String? payload;
  14. }
  15. /// A notification action which triggers a App navigation event
  16. const String navigationActionId = 'id_3';
  17. @pragma('vm:entry-point')
  18. void notificationTapBackground(NotificationResponse notificationResponse) {
  19. // ignore: avoid_print
  20. print('notification(${notificationResponse.id}) action tapped: '
  21. '${notificationResponse.actionId} with'
  22. ' payload: ${notificationResponse.payload}');
  23. if (notificationResponse.input?.isNotEmpty ?? false) {
  24. // ignore: avoid_print
  25. print(
  26. 'notification action tapped with input: ${notificationResponse.input}');
  27. }
  28. }
  29. class NotificationService {
  30. //Singleton pattern
  31. static final NotificationService _notificationService =
  32. NotificationService._internal();
  33. factory NotificationService() {
  34. return _notificationService;
  35. }
  36. NotificationService._internal();
  37. /// Streams are created so that app can respond to notification-related events
  38. /// since the plugin is initialised in the `main` function
  39. final StreamController<ReceivedNotification>
  40. didReceiveLocalNotificationStream =
  41. StreamController<ReceivedNotification>.broadcast();
  42. final StreamController<String?> selectNotificationStream =
  43. StreamController<String?>.broadcast();
  44. //instance of FlutterLocalNotificationsPlugin
  45. final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  46. FlutterLocalNotificationsPlugin();
  47. Future<void> initialize() async {
  48. const AndroidInitializationSettings initializationSettingsAndroid =
  49. AndroidInitializationSettings('@drawable/iclauncher');
  50. //Initialization Settings for iOS devices
  51. final DarwinInitializationSettings initializationSettingsDarwin =
  52. DarwinInitializationSettings(
  53. requestSoundPermission: true,
  54. requestBadgePermission: true,
  55. requestAlertPermission: true,
  56. onDidReceiveLocalNotification:
  57. (int id, String? title, String? body, String? payload) async {
  58. didReceiveLocalNotificationStream.add(
  59. ReceivedNotification(
  60. id: id,
  61. title: title,
  62. body: body,
  63. payload: payload,
  64. ),
  65. );
  66. },
  67. );
  68. final InitializationSettings settings = InitializationSettings(
  69. android: initializationSettingsAndroid,
  70. iOS: initializationSettingsDarwin,
  71. );
  72. await flutterLocalNotificationsPlugin.initialize(
  73. settings,
  74. onDidReceiveNotificationResponse:
  75. (NotificationResponse notificationResponse) {
  76. switch (notificationResponse.notificationResponseType) {
  77. case NotificationResponseType.selectedNotification:
  78. selectNotificationStream.add(notificationResponse.payload);
  79. break;
  80. case NotificationResponseType.selectedNotificationAction:
  81. if (notificationResponse.actionId == navigationActionId) {
  82. selectNotificationStream.add(notificationResponse.payload);
  83. }
  84. break;
  85. }
  86. },
  87. onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
  88. );
  89. }
  90. Future<NotificationDetails> _notificationDetails() async {
  91. const AndroidNotificationDetails androidNotificationDetails =
  92. AndroidNotificationDetails(
  93. 'channel ID',
  94. 'channel name',
  95. playSound: true,
  96. priority: Priority.high,
  97. importance: Importance.high,
  98. );
  99. const NotificationDetails notificationDetails =
  100. NotificationDetails(android: androidNotificationDetails);
  101. return notificationDetails;
  102. }
  103. Future<void> showNotification({
  104. required int id,
  105. required String title,
  106. required String body,
  107. }) async {
  108. final details = await _notificationDetails();
  109. await flutterLocalNotificationsPlugin.show(id, title, body, details);
  110. }
  111. }

字符串

5fjcxozz

5fjcxozz1#

我认为你的iOS初始化问题造成了你的问题。我正在使用这个类...所以请检查这个并更新你的代码。最重要的是检查iOS初始化部分。

  1. class LocalNotificationService {
  2. static final FlutterLocalNotificationsPlugin _notificationsPlugin =
  3. FlutterLocalNotificationsPlugin();
  4. static void initialize() {
  5. final InitializationSettings initializationSettings =
  6. InitializationSettings(
  7. android: AndroidInitializationSettings("@mipmap/transparent_othership"),
  8. iOS: IOSInitializationSettings(),
  9. );
  10. _notificationsPlugin.initialize(initializationSettings,
  11. onSelectNotification: (String? body) async {
  12. if (body != null) {
  13. final json = jsonDecode(body);
  14. json.toString().log();
  15. }
  16. });
  17. }
  18. static void display(RemoteMessage message) async {
  19. try {
  20. // final sound = "notification_sound.wav";
  21. final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  22. final NotificationDetails notificationDetails = NotificationDetails(
  23. android: AndroidNotificationDetails(
  24. 'channel ID',
  25. 'channel name',
  26. importance: Importance.max,
  27. priority: Priority.high,
  28. color: Color.fromARGB(255, 0, 0, 0),
  29. enableLights: false,
  30. playSound: true,
  31. enableVibration: false,
  32. showWhen: false,
  33. // sound: RawResourceAndroidNotificationSound(sound.split('.').first),
  34. ),
  35. //we need to create Resouces folder in
  36. //runner then put the wav file in there
  37. iOS: IOSNotificationDetails(presentSound: true),
  38. );
  39. String title = "";
  40. String body = "";
  41. message.toString().log();
  42. message.notification.toString().log();
  43. message.data.toString().log();
  44. if (message.notification != null) {
  45. title = message.notification?.title ?? "";
  46. body = message.notification?.body ?? "";
  47. } else {
  48. final responseBody = jsonDecode(message.data['body']);
  49. title = responseBody['title'] ?? "";
  50. body = responseBody['body'];
  51. }
  52. await _notificationsPlugin.show(
  53. id,
  54. title,
  55. body,
  56. notificationDetails,
  57. payload: message.data.isEmpty ? null : message.data['body'],
  58. );
  59. } on Exception catch (e) {
  60. debugPrint(e.toString());
  61. }
  62. }
  63. }

字符串

展开查看全部

相关问题