The Talk Object

The Talk object provides utility functions to help use TalkJS.

oneOnOneId

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:

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

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

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}

Type Definitions

AndroidSettings

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});

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.

IOSSettings

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