Flutter Push Notifications
The Flutter SDK makes it easy to integrate TalkJS into your app. This guide shows you how to set up and receive push notifications from TalkJS on Flutter for both iOS and Android.
The guide assumes that you have set up the TalkJS Flutter SDK as shown in the Getting Started guide.
This section guides you through the steps to set up push notifications from TalkJS with Flutter for Android.
If you haven't already,
configure your app to use Firebase.
When configuring your app to use Firebase, you may skip installing firebase_core
and firebase_messaging
(steps 3 and 4 in the
linked tutorial), since the talkjs-flutter
library already
depends on them.
To correctly configure your app for use with Firebase, make sure you run the following command after installing talkjs-flutter
:
1flutterfire configure
Add your Firebase service account private key on the Settings page in your TalkJS dashboard. For a detailed guide on how to do that, see: Configure Firebase Cloud Messaging.
Note: If you will only be using Firebase for push notifications on Android, make sure that when you run
flutterfire configure
, you only select Android and not iOS. If you are using other Firebase services also
on iOS, follow the instructions in the section on sending push notifications on iOS using Firebase.
If your app is targeting Android 13 or higher, you are required to request for a new runtime permission called POST_NOTIFICATIONS
in order for the app to display notifications.
You first need to declare the permission in your app's manifest file (android/app/src/main/AndroidManifest.xml
) by
placing the following line within the <manifest>
tag:
XML1<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
You then need to request for the permission at runtime using flutter_local_notifications
.
Add it as a dependency in your project by running:
1flutter pub add flutter_local_notifications;
You can then import and use it as shown below:
DART1import 'dart:io' show Platform;2import 'package:flutter_local_notifications/flutter_local_notifications.dart';34// Other code...56if (Platform.isAndroid) {7 await FlutterLocalNotificationsPlugin()8 .resolvePlatformSpecificImplementation<9 AndroidFlutterLocalNotificationsPlugin>()!10 .requestNotificationsPermission();11};
You can request for this permission at any point in your app's flow that makes sense for your project and its users.
TalkJS supports sending push notifications to iOS devices using Apple Push Notification service (APNs) or using Firebase.
Regardless of whether you use Apple Push Notifications (APNs) or Firebase, Apple requires that an iOS app requests for the necessary
notification permissions before being able to display push notifications. You can request for these permissions at any point in your
app's flow by using flutter_local_notifications
.
Add it as a dependency in your project by running:
1flutter pub add flutter_local_notifications;
You can then import and use it as shown below:
DART1import 'dart:io' show Platform;2import 'package:flutter_local_notifications/flutter_local_notifications.dart';34// Other code...56if (Platform.isIOS) {7 await FlutterLocalNotificationsPlugin()8 .resolvePlatformSpecificImplementation<9 IOSFlutterLocalNotificationsPlugin>()!10 .requestPermissions(sound: true, alert: true, badge: true);11}
Begin by configuring Apple Push Notifications (APNs) on the TalkJS dashboard. See: Configure Apple Push Notifications.
You then need to augment your iOS app's AppDelegate.m
or AppDelegate.swift
file by placing the following
lines inside the didFinishLaunchingWithOptions
method:
1if (@available(iOS 10.0, *)) {2 [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;3}
Next, you can jump to registering push notification handlers.
By default TalkJS sends push notifications on Android via Firebase, and on iOS using Apple Push Notification service (APNs). However, we also support sending push notification on iOS via Firebase.
If you haven't already, make sure to configure Firebase in your Flutter project
as shown above. When you run flutterfire configure
, make sure to select iOS as one of the platforms to configure. Also,
don't forget to upload your APNs authentication key
on your project's Firebase console.
You then need to add the following configuration entry to your Info.plist
:
1<key>flutter_apns.disable_swizzling</key>2<true/>
The reason for the above entry is because the TalkJS Flutter package depends on the flutter_apns
package,
which by default disables Firebase. That entry prevents the flutter_apns
package from doing so. This ensures
that you can use Firebase on iOS for push notifications as well as Analytics among other Firebase services.
To register push notification handlers, make a call to
Talk.registerPushNotificationHandlers
from inside your app's main.dart
file.
Call the registerPushNotificationHandlers
function before runApp()
, because when your application is stopped—whether by the user swiping away
from the recents screen, or due to low memory on the device—notifications received won't start the full app. This means that no widget code runs, and therefore no notifications are displayed.
Aside from setting up the notification handlers, the function also creates an Android notification channel. Without a notification channel, notifications won't appear on Android devices.
You can also pass an object describing the different iOS permissions you'd like to request.
Below is an example main.dart
file:
DART1import 'dart:io' show Platform;2import 'package:flutter/material.dart';3import 'package:talkjs_flutter/talkjs_flutter.dart';4import 'package:firebase_core/firebase_core.dart';5import 'firebase_options.dart';6import 'package:flutter_local_notifications/flutter_local_notifications.dart';78Future<void> main() async {9 WidgetsFlutterBinding.ensureInitialized();1011 // Request push notification permissions12 // You can request these permissions at any point in your application.13 if (Platform.isAndroid) {14 await FlutterLocalNotificationsPlugin()15 .resolvePlatformSpecificImplementation<16 AndroidFlutterLocalNotificationsPlugin>()!17 .requestNotificationsPermission();18 } else if (Platform.isIOS) {19 await FlutterLocalNotificationsPlugin()20 .resolvePlatformSpecificImplementation<21 IOSFlutterLocalNotificationsPlugin>()!22 .requestPermissions(sound: true, alert: true, badge: true);23 }2425 /*26 If you use Firebase on iOS, then remove this if statement and call27 `Firebase.initializeApp` for both iOS and Android.28 */29 if (Platform.isAndroid) {30 await Firebase.initializeApp(31 options: DefaultFirebaseOptions.currentPlatform,32 );33 }3435 await Talk.registerPushNotificationHandlers(36 androidSettings: const AndroidSettings(37 channelId: 'com.example.talkjsflutter.messages',38 channelName: 'Messages',39 ),40 iosSettings: const IOSSettings(useFirebase: false),41 );4243 runApp(const MyApp());44}
For your user actually to receive push notifications, use the enablePushNotifications
parameter in the Session
constructor, to enable or turn off push notifications for that particular device.
1final session = Session(2 appId: '<APP_ID>',3 enablePushNotifications: true,4);
Users who have enablePushNotifications
set to true
have message notifications displayed whenever the app is in the background.
If you have any issues receiving push notifications you can reach out to us via our support chat.