Skip to content

Conversation

@bugnano
Copy link
Collaborator

@bugnano bugnano commented Dec 4, 2024

See https://talkjs.slack.com/archives/C02PH434FAT/p1733135037788469

This PR adds the registerBackgroundHandler option to the AndroidSettings that allows to not register the background handler (defaults to true).

It also adds the Talk.handleFCMBackgroundMessage function, that is to be called inside a background notification handler.

import 'package:flutter/material.dart';
import 'package:talkjs_flutter/talkjs_flutter.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'firebase_options.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (Platform.isAndroid) {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
  }

  await Talk.registerPushNotificationHandlers(
    androidSettings: const AndroidSettings(
      channelId: 'com.example.talkjsflutter.messages',
      channelName: 'Messages',
      registerBackgroundHandler: false,
    ),
    iosSettings: const IOSSettings(useFirebase: false),
  );

  FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);

  runApp(const MyApp());
}

@pragma("vm:entry-point")
Future<void> myBackgroundMessageHandler(RemoteMessage firebaseMessage) async {
  // Call the TalkJS-specific push notification handler
  final notificationHandled = await Talk.handleFCMBackgroundMessage(firebaseMessage);

  if (!notificationHandled) {
    // Handle non-TalkJS originated push notification
  }
}

@bugnano bugnano requested a review from vickz84259 December 4, 2024 08:30
Copy link
Collaborator

@vickz84259 vickz84259 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Everything checks out.

Sidenote: The diff view on Github for the changes in notification.dart was almost impossible to tell how things had changed. I ended up just copying the raw file into VSCode and comparing with the previous code to understand.

@bugnano bugnano force-pushed the fix/non-talkjs-notifications branch from e88828a to 665a1ca Compare December 4, 2024 11:58
Copy link
Collaborator

@vickz84259 vickz84259 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor suggestion. Feel free to merge and release afterwards.

Co-authored-by: Victor Otieno <victor@slick.co.ke>
@bugnano bugnano merged commit a138974 into main Dec 4, 2024
@bugnano bugnano deleted the fix/non-talkjs-notifications branch December 4, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants