Skip to main content

smf_firebase_messaging 0.6.0 copy "smf_firebase_messaging: ^0.6.0" to clipboard
smf_firebase_messaging: ^0.6.0 copied to clipboard

The SMF module of Firebase Cloud Messaging for the Flutter app that SMF generates. The app receives push messages and hears of the taps on them.

example/README.md

Generate a Flutter app that receives push messages #

firebase_messaging is the module of the SMF CLI with which the app receives push messages through Firebase Cloud Messaging. Choose it with -m:

dart pub global activate smf_flutter_cli
smf create my_app -m firebase_messaging --no-input

The push role shows a message that arrives in the foreground as a notification of the app, so it needs a provider of the notifications, which in turn needs one of the preferences. Firebase Cloud Messaging works on the Firebase app. smf create adds all three:

Adding firebase_core: a dependency of firebase_messaging.
Adding flutter_core: the only provider of the app entry role, which every app needs.
Adding local_notifications: the only provider of the notifications role, which firebase_messaging requires.
Adding shared_preferences: the only provider of the preferences role, which local_notifications requires.

The module adds firebase_messaging to the pubspec.yaml of the app, and lib/core/push/firebase_push_service.dart with the push service on FirebaseMessaging. The push role adds lib/core/push/app_push.dart with appPush, which the rest of the app uses. bootstrap() starts the push messages before the first frame, once Firebase and the notifications started, without asking the user anything:

Future<void> bootstrap() async {
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  await initPreferences();
  await initNotifications();
  await initPush();
}

The code of the app sends the address of the device to its backend, and opens the screen that a tapped message names:

import 'core/push/app_push.dart';

void listenToPushMessages(void Function(String screen) open) {
  void sendAddress() {
    final address = appPush.address.value;
    if (address != null) backend.registerDevice(address);
  }

  // The address may be there already, or come later.
  sendAddress();
  appPush.address.addListener(sendAddress);
  appPush.taps.listen((message) {
    if (message.data['screen'] case final screen?) open(screen);
  });
  if (appPush.launchMessage?.data['screen'] case final screen?) open(screen);
}

The address is the registration token of Firebase Cloud Messaging. On iOS it comes once the device has a token of the Apple Push Notification service, and Firebase sends to iOS only with a key of that service, which you upload in the Firebase console. The README of the app tells how, and how to send the device a test message.

The documentation has more on the firebase_messaging module.

0
likes
160
points
--
downloads

Documentation

Documentation
API reference

Publisher

verified publishersaymyframe.com

Weekly Downloads

The SMF module of Firebase Cloud Messaging for the Flutter app that SMF generates. The app receives push messages and hears of the taps on them.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#flutter #firebase #push-notifications #firebase-messaging #template

License

Apache-2.0 (license)

Dependencies

mason, smf_contracts, smf_firebase_core

More

Packages that depend on smf_firebase_messaging