usersina
usersina

Reputation: 1845

Flutter issue separating Firebase Cloud Messaging functions from main.dart

I have a flutter application that uses Firebase Cloud Messaging to display push notifications, pretty basic.

The main.dart feels so heavy so I added a push_notification_service.dart file to handle the notifications there. However in doing so I couldn't use the notifications data.

I'm getting the following in debug console: (EDIT: When using .then() only. Await doesn't work)

Fatal: failed to find callback, followed by a print statement with my notification data.

Here's the push_notification_service.dart

import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';

class PushNotificationService {
  final FirebaseMessaging _fcm;

  PushNotificationService(this._fcm);
  Future initialise() async {
    String initMessage = "default";
    if (Platform.isIOS) {
      // Request permission if on IOS
      _fcm.requestNotificationPermissions(IosNotificationSettings());
    }

    _fcm.configure(
      // Called when the app is in the foreground and a push notif is received
      onMessage: (Map<String, dynamic> message) async {
        print("Message -Foreground- received: $message"); // This is printed successfully
        initMessage = message['notification']['title'];
      },

      // Called when the app is completely closed and it's opened from
      // the push notification directly
      onLaunch: (Map<String, dynamic> message) async {
        print("Message -Closed- received: $message"); // This is printed successfully
        initMessage = message['notification']['title'];
      },

      // Called when the app is in the background and it's opened from
      // the push notification
      onResume: (Map<String, dynamic> message) async {
        print("Message -Background- received: $message"); // This is printed successfully
        initMessage = message['notification']['title'];
      },
    );

    return initMessage;
  }
}

Here's the main.dart:

import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import './push_notification_service.dart';    

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  final FirebaseMessaging _fcm = FirebaseMessaging();
  String title = "Notif Title";
  String helperText = "Notif Text";

  @override
  void initState() {
    super.initState();
    title = await PushNotificationService(_fcm).initialise();
    // The following line didn't work too:
    // PushNotificationService(_fcm).initialise().then((data) { setState(...) } )
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp( home: Text(title) );
  }

}

I ultimately want to assign the notification title to the title variable in main.dart

Upvotes: 0

Views: 503

Answers (2)

usersina
usersina

Reputation: 1845

I've figured it out, I was using the class without initializing an instance.

Here's the code:

final NotifService _notif = NotifService(); // PushNotificationService (just a rename)

@override
void initState() {
  super.initState();
  _notif.initialise();
}

Upvotes: 0

Crazzygamerr
Crazzygamerr

Reputation: 356

Return type of initialise() should be Future<String>. In the initState, use this statement:

PushNotificationService(_fcm).initialise().then((value) => title = value);

You can't use await in initState(), as it is not an async function. Read up on some docs, if you want to use await, then call a seperate function from initState():

testFunc() async {
  title = await PushNotificationService(_fcm).initialise();
}

Upvotes: 0

Related Questions