后台 FCM 触发时生成 Flutter 本地通知

Posted

技术标签:

【中文标题】后台 FCM 触发时生成 Flutter 本地通知【英文标题】:Generate Flutter Local Notification when background FCM Triggers 【发布时间】:2020-05-30 20:18:20 【问题描述】:

基本上我试图在 FCM onBackgroundMessage 触发时发出通知。 在执行此操作时,我在 FlutterLocalNotification 插件的.show(..) 方法中获得了NullPointerException

这是我的代码:

 FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async 
        print("onMessage: $message");
      ,
      onBackgroundMessage: messageHandle,
      onLaunch: (Map<String, dynamic> message) async 
        print("onLaunch: $message");
      ,
      onResume: (Map<String, dynamic> message) async 
        print("onResume: $message");
      ,
    );
Future<dynamic> messageHandle(Map<String, dynamic> message) async
  if (message.containsKey("data")) 
    Map data = message['data'];
    String topic;
    if (data.containsKey("topic")) topic = data['topic'];
    if (topic == "VendorNewTrips") 
      String amount = data['amount'];
      String vehicle = data['vehicle'];
      String tripType = data['tripType'];
      String pickUpDate= data['pickUpDate'];
      String pickUpTime = data['pickUpTime'];
      String locations= data['locations'];
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      new FlutterLocalNotificationsPlugin();

      new androidInitializationSettings('app_icon');
      var bigTextStyleInformation = BigTextStyleInformation(
          'Locations: <b>$locations.replaceAll("\$", " to ")</b><br>Vehicle: <b>$vehicle</b><br>Trip Type: <b>$tripType</b><br>Pick-Up Date: <b>$pickUpDate</b><br>Pick-Up Time: <b>$pickUpTime</b>',
          htmlFormatBigText: true,
          contentTitle: 'Amount:- <b>Rs $amount</b>',
          htmlFormatContentTitle: true,
          summaryText: 'Trip Details',
          htmlFormatSummaryText: true);
      var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          '1687497218170948721x8',
          'New Trips Notification ',
          'Notification Channel for vendor. All the new trips notifications will arrive here.',
          style: AndroidNotificationStyle.BigText,
          styleInformation: bigTextStyleInformation);
      var platformChannelSpecifics =
      NotificationDetails(androidPlatformChannelSpecifics, null);

      flutterLocalNotificationsPlugin.show(5, 'Let\'s Get Wride!',
          'You Have Got A New Trip!', platformChannelSpecifics,);
    
  
  return null;


E/MethodChannel#dexterous.com/flutter/local_notifications(15092): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(15092): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:208)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:180)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:693)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:785)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:714)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at android.os.Looper.loop(Looper.java:190)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at android.app.ActivityThread.main(ActivityThread.java:7211)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
E/MethodChannel#dexterous.com/flutter/local_notifications(15092):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
E/flutter (15092): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null)
E/flutter (15092): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (15092): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter (15092): <asynchronous suspension>
E/flutter (15092): #2      AndroidFlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/platform_flutter_local_notifications.dart:135:21)
E/flutter (15092): #3      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:93:13)
E/flutter (15092): #4      messageHandle (package:wride/SplashScreen/SplashScreen.dart:155:39)
E/flutter (15092): #5      _fcmSetupBackgroundChannel.<anonymous closure> (package:firebase_messaging/firebase_messaging.dart:38:30)
E/flutter (15092): #6      MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:402:55)
E/flutter (15092): #7      MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:370:54)
E/flutter (15092): #8      _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:200:33)
E/flutter (15092): #9      _invoke3.<anonymous closure> (dart:ui/hooks.dart:303:15)
E/flutter (15092): #10     _rootRun (dart:async/zone.dart:1126:13)
E/flutter (15092): #11     _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (15092): #12     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (15092): #13     _invoke3 (dart:ui/hooks.dart:302:10)
E/flutter (15092): #14     _dispatchPlatformMessage (dart:ui/hooks.dart:162:5)
E/flutter (15092): 

我想在后台消息触发时制作自己的时尚通知,因为 FCM 不支持通知样式。 请帮忙!

编辑:我查看了插件 FlutterLocalNotification 并知道NotificationDetails.java Line 151 Interger iconResourceID 一直为空。我现在不知道该怎么办。

【问题讨论】:

【参考方案1】:

好的,我知道错在哪里了。我忘了用 app_icon 初始化颤振本地通知插件。我改变了我的messageHandle 方法,它现在正在工作。

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          new FlutterLocalNotificationsPlugin();
      var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          '1687497218170948721x8',
          'New Trips Notification ',
          'Notification Channel for vendor. All the new trips notifications will arrive here.',
          style: AndroidNotificationStyle.BigText,
          icon: "app_icon",
          styleInformation: BigTextStyleInformation(
            "hanji hogya?",//  'Locations: <b>$locations.replaceAll("\$", " to ")</b><br>Vehicle: <b>$vehicle</b><br>Trip Type: <b>$tripType</b><br>Pick-Up Date: <b>$pickUpDate</b><br>Pick-Up Time: <b>$pickUpTime</b>',
              htmlFormatBigText: true,
              contentTitle: 'Amount:- <b>Rs 22000000</b>',
              htmlFormatContentTitle: true,
              summaryText: 'Trip Details',
              htmlFormatSummaryText: true));

【讨论】:

这是您更正的完整代码吗?我想你忘了提到你初始化 FlutterLocalNotification 的部分! 问题和答案,都帮助了我谢谢 如果您同时拥有 FCM 插件和 this 可能会发生冲突。 我尝试了同样的方法,但对我来说只是显示了默认的 fcm 消息。它似乎覆盖了本地通知。您是否像他们在文档中所说的那样在 MainActivity 中注册了 firebase 消息? @Rocx 我做到了。就我而言,我从服务器发送的通知中有一些消息和样式。但它必须是纯数据通知(尝试删除除数据部分之外的所有内容)。

以上是关于后台 FCM 触发时生成 Flutter 本地通知的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:当应用程序在后台时使用路径提供程序

Flutter ios 设备不会从 FCM 通知触发 onMessage。实施 APN 时 Sendtodevice 失败

Flutter IOS 通知的 FCM 在应用程序处于后台或终止时不显示

Flutter 和 FCM(Firebase 云消息传递)onMessage、onResume 和 onLaunch 在单击通知时未触发(包:firebase_messaging 7.0.0)

Flutter 本地通知 - 当应用程序在后台运行时,ZonedSchedule 通知不起作用

Flutter - 在 IOS 上使用 FCM 的后台通知