The Talk Object

The Talk object provides utility functions to help use TalkJS.

oneOnOneId

DART
static 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
final ids = [me, other];
ids.sort();
final encoded = json.encode(ids);
final hash = sha1.convert(utf8.encode(encoded)).toString().toLowerCase(); // as lowercase hex
return hash.substring(0, 20);

Parameters

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

registerPushNotificationHandlers

DART
static Future<void> registerPushNotificationHandlers({
AndroidChannel? androidChannel,
IOSPermissions? iosPermissions,
});

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 killed.

Parameters

androidChannel
(optional)AndroidChannel?

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

iosPermissions
(optional)IOSPermissions?

An Object indicating the different iOS permissions that can be requested.

Returns

Future<void>

Example

DART
// Inside your app's main.dart
import 'package:flutter/material.dart';
import 'package:talkjs_flutter/talkjs_flutter.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}
await Talk.registerPushNotificationHandlers(
androidChannel: const AndroidChannel(
channelId: 'com.example.talkjsflutter.messages',
channelName: 'Messages',
),
iosPermissions: const IOSPermissions(
alert: true,
badge: true,
sound: true,
),
);
runApp(const MyApp());
}

Type Definitions

AndroidChannel

DART
const AndroidChannel({
required String channelId,
required String channelName,
bool? badge,
String? channelDescription,
bool? lights,
Color? lightColor,
String playSound = 'default',
AndroidImportance? importance,
AndroidVisibility? visibility,
bool? vibrate,
Int64List? vibrationPattern,
});

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.

IOSPermissions

DART
const IOSPermissions({
bool alert = true,
bool badge = true,
bool sound = true,
});

This object indicates the various push notification related permissions that can be requested.

Parameters

alert
(optional)bool

The ability to display notification alerts.

Default: true

badge
(optional)bool

The ability to update the app's badge.

Default: true

sound
(optional)bool

The ability to play notification sounds.

Default: true

AndroidImportance

enum
HIGH
DEFAULT
LOW
MIN
NONE

AndroidVisibility

enum
PRIVATE
PUBLIC
SECRET