Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Flutter Local Notification when background FCM Triggers

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.

like image 728
Jagraj Singh Avatar asked Mar 02 '23 19:03

Jagraj Singh


1 Answers

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));
like image 176
Jagraj Singh Avatar answered Apr 09 '23 16:04

Jagraj Singh