The Talk Object
The Talk
object provides utility functions to help use TalkJS.
DART1static 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:
- Take two ids of users and put them in an array
- Sort them lexicographically
- JSON encode them
- Hash the result using SHA1, return the first 20 characters
In pseudocode, this is what this function does:
DART1final ids = [me, other];2ids.sort();34final encoded = json.encode(ids);5final hash = sha1.convert(utf8.encode(encoded)).toString().toLowerCase(); // as lowercase hex67return hash.substring(0, 20);
me String | A string containing the user ID. |
other String | Another string containing the user ID. |
DART1static 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.
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.
Future<void>
DART1// Inside your app's main.dart2import 'package:flutter/material.dart';3import 'package:talkjs_flutter/talkjs_flutter.dart';4import 'package:firebase_core/firebase_core.dart';5import 'firebase_options.dart';67Future<void> main() async {8 WidgetsFlutterBinding.ensureInitialized();910 if (Platform.isAndroid) {11 await Firebase.initializeApp(12 options: DefaultFirebaseOptions.currentPlatform,13 );14 }1516 await Talk.registerPushNotificationHandlers(17 androidSettings: const AndroidSettings(18 channelId: 'com.example.talkjsflutter.messages',19 channelName: 'Messages',20 ),21 iosSettings: const IOSSettings(useFirebase: false),22 );2324 runApp(const MyApp());25}
DART1static 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
.
firebaseMessage
: RemoteMessageThe remote FCM message payload.
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.
DART1import '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';67Future<void> main() async {8 WidgetsFlutterBinding.ensureInitialized();910 if (Platform.isAndroid) {11 await Firebase.initializeApp(12 options: DefaultFirebaseOptions.currentPlatform,13 );14 }1516 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 );2425 FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);2627 runApp(const MyApp());28}2930("vm:entry-point")31Future<void> myBackgroundMessageHandler(RemoteMessage firebaseMessage) async {32 // Call the TalkJS-specific push notification handler33 final notificationHandled = await Talk.handleFCMBackgroundMessage(firebaseMessage);3435 if (!notificationHandled) {36 // Handle non-TalkJS originated push notification37 }38}
DART1const 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.
channelId
: StringThe unique id of the channel.
channelName
: StringThe 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): StringSound 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
DART1const IOSSettings({ bool useFirebase = false });
This object configures the appropriate iOS push notification service.
useFirebase
(optional): boolWhether your app is using Firebase push notifications on iOS.
Default: false