Home > Net >  I'm trying to get firebase cloud messaging for my mobile app but I keep getting platform except
I'm trying to get firebase cloud messaging for my mobile app but I keep getting platform except

Time:07-15

Hi I wanted to add cloud messaging for my app. I've done all the necessary steps for integrating the Firebase with my app.Then I followed steps on a youtube video to get firebase cloud messaging work for my app and then these errors popped up. The app was working fine before adding all the firebase related code. Now I just get a white screen. This is the my main.dart

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'splashScreen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel('high_importance_channel',
    'High Importance Notifications',
importance: Importance.high,
playSound: true);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async{
  await Firebase.initializeApp();
  print('A bg message just showed up: ${message.messageId}');
}

Future<void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState(){
    super.initState();
    FirebaseMessaging.onMessage.listen((RemoteMessage message){
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;
    if(notification!=null && android!=null){
      flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id,
            channel.name,
            color: Colors.blue,
            playSound: true,
            icon: '@ipmap/ic_launcher',

          )
        )
      );
    }
    });
    
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    print('A new onMessageOpenedApp event was published');
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;
    if(notification!=null && android!=null){
      showDialog
        (context: context,
          builder: (_){
        return AlertDialog(
          title: Text(notification.title??''),
          content: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(notification.body??'')
              ],
            ),
          ),
        );
      });
    }
    }
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: SplashScreen(),
    );
  }
}

I was getting another error before this but that got solved after I changed the dependencies so that will not be the problem . And this is the error that I'm getting

E/flutter ( 3318): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter ( 3318): #0      FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:205:7)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #1      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #2      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #3      Firebase.initializeApp (package:firebase_core/src/firebase.dart:40:31)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): #4      main (package:activepeers_app_internship/main.dart:23:3)
E/flutter ( 3318): <asynchronous suspension>
E/flutter ( 3318): 
I/TRuntime.CctTransportBackend( 3318): Making request to: https://firebaselogging.googleapis.com/v0cc/log/batch?format=json_proto3
D/NetworkSecurityConfig( 3318): No Network Security Config specified, using platform default
I/TRuntime.CctTransportBackend( 3318): Status Code: 200

CodePudding user response:

I was able to fix this issue by updating all firebase dependencies.Below commands worked for me.

flutter pub outdated
flutter pub upgrade
  • Related