skip to Main Content

I am busy trying to set up push notifications in my Flutter app using the cloud messaging service from Firebase

I am following the a Udemy lecture (if you have a Udemy subscription you can find it here) and have stumbled across an error that I can’t seem to find replicated anywhere else online. The code that I am using to retrieve the FCM registration token is as follows:

void _setupPushNotifications() async {
    try {
      
      final fcm = FirebaseMessaging.instance;
      final settings = await fcm.requestPermission();
      print(settings.authorizationStatus);
      final token = await fcm.getToken();
      print('token is: $token');
 
    } catch (e, stackTrace) {
      print('Error setting up push notifications: $e');
      print(stackTrace);
    }
  }
 
  @override
  void initState() {
    _setupPushNotifications();
    super.initState();
  }

This is exactly the same as it is done in the lecture, and this same code can be found in many examples/tutorials online and in the documentation. However, when I call fcm.getToken(), I unfortunately get an error with very little detail. The result in the debug console of the print statements, error, and stack trace can be seen below:

Restarted application in 308ms.
flutter: AuthorizationStatus.authorized
flutter: Error setting up push notifications: [firebase_messaging/unknown] An unknown error has occurred.
flutter: #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18) platform_channel.dart:334
<asynchronous suspension>
#2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:534:43) platform_channel.dart:534
<asynchronous suspension>
#3 MethodChannelFirebaseMessaging.getToken (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:248:11) method_channel_messaging.dart:248
<asynchronous suspension>
#4 _ChatScreenState._setupPushNotifications (package:chat_app/screens/chat.dart:20:21) chat.dart:20
<asynchronous suspension>

So as you can see the request is authorized just fine and everything works up until that point, but as soon as I try to use the getToken() method, an error is immediately thrown.

I have tried searching online to figure out why this might be. This stack overflow post led me to have a look at my iOS key in my Google Cloud Console, where I noticed that the Firebase Cloud Messaging API had not been selected for this key. However, after having selected this, saved, and waited much more than the suggested 5 minutes, it did not seem to make any difference.

I have also restarted my app, tried a different IOS simulator, and run flutter clean and flutter doctor. I don’t really see where else to go from here as there is so little information on what the error could be.

2

Answers


  1. Enable push notifications capabilities through xcode.

    1. Open your project on xcode
    2. In project navigator select your project
    3. In project editor select target then go to Signing & Capabilities tab

    Add a capability to a target

    1. Click + Capabilities
    2. Search for Push Notifications then add it
    3. Try to run again.

    Reference: Enable push notifications – Xcode Help

    Login or Signup to reply.
  2. Have you tried on iOS physical device? Because I have same issue but it’s only happened on Simulator.

    Probably related with firebase flutter docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search