The Talk Object

The Talk object provides utility functions to help use TalkJS.

oneOnOneId

DART
1static String oneOnOneId(String me, String other)

Use this helper method to predictably compute a Conversation ID based on participants' ids in the given conversation. Use this method if you want to simply create a conversation between two users, not related to a particular product, order or transaction. The order of the parameters does not matter. For example, Talk.oneOnOneId("a", "b") yields the same result as Talk.oneOnOneId("b", "a"). This method takes the following steps:

  1. Take two ids of users and put them in an array
  2. Sort them lexicographically
  3. JSON encode them
  4. Hash the result using SHA1, return the first 20 characters

In pseudocode, this is what this function does:

DART
1final ids = [me, other];
2ids.sort();
3
4final encoded = json.encode(ids);
5final hash = sha1.convert(utf8.encode(encoded)).toString().toLowerCase(); // as lowercase hex
6
7return hash.substring(0, 20);

Parameters

me
String
A string containing the user ID.
other
String
Another string containing the user ID.

registerPushNotificationHandlers

DART
1static Future<void> registerPushNotificationHandlers({
2 AndroidSettings? androidSettings,
3 IOSSettings? iosSettings,
4});

This function sets up the push notification handlers. It is meant to be called in the app's main() function before runApp(). This is critical to ensuring that notifications are received and handled properly especially when the app is stopped.

Note: For versions 0.9.3 and older, calling this function would also implicitly request permissions on iOS. From version 0.10.0, this is no longer the case.

Parameters

androidSettings
(optional)AndroidSettings?

An object describing the Android Notification Channel to be created. Without a channel, notifications will not work.

iosSettings
(optional)IOSSettings?

An object indicating the different iOS push notification settings.

Returns

Future<void>

Example

DART
1// Inside your app's main.dart
2import 'package:flutter/material.dart';
3import 'package:talkjs_flutter/talkjs_flutter.dart';
4import 'package:firebase_core/firebase_core.dart';
5import 'firebase_options.dart';
6
7Future<void> main() async {
8 WidgetsFlutterBinding.ensureInitialized();
9
10 if (Platform.isAndroid) {
11 await Firebase.initializeApp(
12 options: DefaultFirebaseOptions.currentPlatform,
13 );
14 }
15
16 await Talk.registerPushNotificationHandlers(
17 androidSettings: const AndroidSettings(
18 channelId: 'com.example.talkjsflutter.messages',
19 channelName: 'Messages',
20 ),
21 iosSettings: const IOSSettings(useFirebase: false),
22 );
23
24 runApp(const MyApp());
25}

handleFCMBackgroundMessage

DART
1static Future<bool> handleFCMBackgroundMessage(RemoteMessage firebaseMessage);

This function handles TalkJS originated push notifications on Android. It is expected to be called inside a custom FirebaseMessaging.onBackgroundMessage handler.

Note: In order to be able to use this function, you have to call the Talk.registerPushNotificationHandlers function with the registerBackgroundHandler: false androidSettings option, and manually register FirebaseMessaging.onBackgroundMessage.

Parameters

firebaseMessage
RemoteMessage

The remote FCM message payload.

Returns

Future<bool>

The return value is true if the push notification was TalkJS originated and handled by this function. A false return value means that the push notification was not TalkJS originated and not handled by this function.

Example

DART
1import 'package:flutter/material.dart';
2import 'package:talkjs_flutter/talkjs_flutter.dart';
3import 'package:firebase_core/firebase_core.dart';
4import 'package:firebase_messaging/firebase_messaging.dart';
5import 'firebase_options.dart';
6
7Future<void> main() async {
8 WidgetsFlutterBinding.ensureInitialized();
9
10 if (Platform.isAndroid) {
11 await Firebase.initializeApp(
12 options: DefaultFirebaseOptions.currentPlatform,
13 );
14 }
15
16 await Talk.registerPushNotificationHandlers(
17 androidSettings: const AndroidSettings(
18 channelId: 'com.example.talkjsflutter.messages',
19 channelName: 'Messages',
20 registerBackgroundHandler: false,
21 ),
22 iosSettings: const IOSSettings(useFirebase: false),
23 );
24
25 FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);
26
27 runApp(const MyApp());
28}
29
30("vm:entry-point")
31Future<void> myBackgroundMessageHandler(RemoteMessage firebaseMessage) async {
32 // Call the TalkJS-specific push notification handler
33 final notificationHandled = await Talk.handleFCMBackgroundMessage(firebaseMessage);
34
35 if (!notificationHandled) {
36 // Handle non-TalkJS originated push notification
37 }
38}

Type Definitions

AndroidSettings

DART
1const AndroidSettings({
2 required String channelId,
3 required String channelName,
4 bool? badge,
5 String? channelDescription,
6 bool? lights,
7 Color? lightColor,
8 String playSound = 'default',
9 AndroidImportance? importance,
10 AndroidVisibility? visibility,
11 bool? vibrate,
12 Int64List? vibrationPattern,
13 bool? registerBackgroundHandler,
14});

This object describes the properties of the Notification Channel on Android.

Parameters

channelId
String

The unique id of the channel.

channelName
String

The user visible name of the channel. The recommended maximum length is 40 characters; the value may be truncated if it is too long.

badge
(optional)bool?

Sets whether badges are enabled for the channel. Defaults to true.

This setting cannot be overridden once the channel is created.

channelDescription
(optional)String?

This is the description that the user sees in the system settings. The recommended maximum length is 300 characters; the value may be truncated if it is too long. Default: null

lights
(optional)bool?

Sets whether notifications posted to this channel should display notification lights, on devices that support that feature. Defaults to true.

This setting cannot be overridden once the channel is created.

lightColor
(optional)Color?

If lights are enabled (via lights), sets/overrides the light color for notifications posted to this channel.

This setting cannot be overridden once the channel is created.

playSound
(optional)String

Sound to play when the notification is shown.

Value of 'default' plays the default sound; an empty string disables the notification sound.

It can be set to a custom sound such as 'my_sound'. It will look for the my_sound.mp3 audio file in [project_root]/android/app/src/main/res/raw directory and play it.

Default: 'default'

importance
(optional)AndroidImportance?

The importance of the channel. This controls how interruptive notifications posted to this channel are. Read more..

Default: AndroidImportance.HIGH

visibility
(optional)AndroidVisibility?

Sets whether notifications posted to this channel appear on the lockscreen or not, and if so, whether they appear in a redacted form.

vibrate
(optional)bool?

Creates the default vibration pattern if true. Default: true

vibrationPattern
(optional)Int64List?

Sets/overrides the vibration pattern for notifications posted to this channel. The pattern in milliseconds. Must be an even amount of numbers.

registerBackgroundHandler
(optional)bool?

Automatically registers the FirebaseMessaging.onBackgroundMessage handler. Default: true

IOSSettings

DART
1const IOSSettings({ bool useFirebase = false });

This object configures the appropriate iOS push notification service.

Parameters

useFirebase
(optional)bool

Whether your app is using Firebase push notifications on iOS.

Default: false

AndroidImportance

enum
HIGH
DEFAULT
LOW
MIN
NONE

AndroidVisibility

enum
PRIVATE
PUBLIC
SECRET