4

Basically I am trying to make a notification when FCM onBackgroundMessage Triggers. While doing this I am getting a NullPointerException at the .show(..) method of FlutterLocalNotification Plugin.

Here is my Code:

 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): 

I want to make my own stylish notification when background message triggers because FCM doesnot support Notification Styles. Please Help!

Edit: I looked into the plugin FlutterLocalNotification and came to know that the NotificationDetails.java Line 151 Interger iconResourceID is null all the time. I don't know what to do now.

Jagraj Singh
  • 4,031
  • 4
  • 15
  • 33

1 Answers1

7

Okay, I got to know where is the mistake. I forgot to initialize the flutter local notification plugin with the app_icon. I changed my messageHandle method and it is working now.

  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));
Jagraj Singh
  • 4,031
  • 4
  • 15
  • 33
  • 2
    Is this the complete code of your correction? I think you forgot to mention the part in which you initialized the FlutterLocalNotification! – Arshdeep Singh Jun 15 '20 at 13:56
  • Question and the answer, both helped me Thanks – Raja C Jul 07 '20 at 13:35
  • 1
    If you have both FCM plugin and this it might conflict. – Oliver Dixon Aug 16 '20 at 14:39
  • Naa it doesn't, it has been working perfectly since then. – Jagraj Singh Aug 17 '20 at 11:32
  • 1
    I tried the same but for me just the default fcm message is showing. It seems it overwrites the local notifications. Did you register firebase messaging in the MainActivity like they say in their docs? – Chris Nov 29 '20 at 01:39
  • I hope question and answer contain all the details to make it work. I am also struggling with it. – Roxx Dec 10 '20 at 14:37
  • @Chris did you managed to get it working? I am also facing the issue. – Roxx Dec 10 '20 at 16:37
  • 1
    @Rocx I did. In my case I had some message and styling stuff in the notification I sent from the server. But it must be a data-only notification (try removing everything but the data part). – Chris Dec 10 '20 at 16:43
  • @Chris i also need a similar thing but still finding it hard to get it working. The strange thing is not many articles are available. Do you have any github/SO link for your code – Roxx Dec 10 '20 at 16:55
  • @Rocx sorry, it's not a public repo. Create a new question here and reference me and I might have a look at it and copy some snippets. – Chris Dec 10 '20 at 18:44
  • @Chris Question is already raised. https://stackoverflow.com/questions/65166526/flutter-fcm-fatal-failed-to-find-callback – Roxx Dec 11 '20 at 04:15