Push Notification
Push notification is a very important aspect of a chat application. Taptalk.io provides a convenient way to implement push notifications so you can easily manage them according to your needs. If you implement Taptalk.io with the UI implementation method, incoming notifications will be handled within our library and will be automatically shown to the user. If you prefer having full control over the push notifications, the core implementation method can be used.
Before you get a notification from your message, you must ensure that the following steps are setup properly in your application. The first step is to create your project on Firebase Console and have the google-service.json file in your application project. The next step is to register the FCM Server Key and Sender ID on the Taptalk.io dashboard.

Step 1: Add Firebase Cloud Messaging and Google Play Service Dependencies

Implement Google Play Service dependency to your build.grade project file.
build.gradle (Project)
1
dependencies {
2
classpath 'com.google.gms:google-services:$gms_version'
3
}
Copied!
Apply google play service plugin and implement firebase messaging dependency to your app-level build.gradle
app module build.gradle
1
apply plugin: 'com.google.gms.google-services'
2
3
dependencies {
4
implementation 'com.google.firebase:firebase-messaging:$firebase_version'
5
}
Copied!
If you have implemented your own push notification to your app, you may add this code in your FirebaseMessagingService class to let Taptalk handle the chat notification.
Java
Kotlin
1
public class YourFirebaseMessagingService extends FirebaseMessagingService {
2
3
@Override
4
public void onMessageReceived(RemoteMessage remoteMessage) {
5
super.onMessageReceived(remoteMessage);
6
if (TapTalk.isTapTalkNotification(remoteMessage)) {
7
TapTalk.handleTapTalkPushNotification(remoteMessage);
8
} else {
9
// Your own notification here
10
}
11
}
12
13
@Override
14
public void onNewToken(String s) {
15
super.onNewToken(s);
16
TapTalk.saveFirebaseToken(s);
17
TapTalk.updateFirebaseTokenToServer(new TapCommonListener() {
18
});
19
}
20
}
Copied!
1
class YourFirebaseMessagingService : FirebaseMessagingService() {
2
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
3
super.onMessageReceived(remoteMessage)
4
if (TapTalk.isTapTalkNotification(remoteMessage!!)) {
5
TapTalk.handleTapTalkPushNotification(remoteMessage)
6
} else {
7
// Your own notification here
8
}
9
}
10
11
override fun onNewToken(s: String?) {
12
super.onNewToken(s)
13
TapTalk.saveFirebaseToken(s)
14
TapTalk.updateFirebaseTokenToServer(object : TapCommonListener() {})
15
}
16
}
Copied!
Otherwise, if you don't handle your own app notification, you can add Taptalk default Firebase Service to your Manifest file.AndroidManifest.xml
AndroidManifest.xml
1
<service android:name="io.taptalk.TapTalk.Firebase.TapFirebaseMessagingService">
2
<intent-filter>
3
<action android:name="com.google.firebase.MESSAGING_EVENT" />
4
</intent-filter>
5
</service>
Copied!

Step 2: Create Project and Get google-service.json File

Before getting notifications, you are required to add firebase to your android project by having a google-service.json file. If the file is already in the folder in your project, no additional steps are needed and you can skip Step 1. If it isn't, you can easily add the file to your project in the way below.
There are 2 options to add the google-service.json file, the first is through the Firebase Console. You will be asked to create and register your project to get the google-service.json file. The second option is using Firebase Assistant where you have to add the plugin in your Android Studio then log in with a google account to connect and configure. For the second option, the google-service.json file will not be needed to be added manually, Firebase Assistant will help you to add it to your project. For further instruction for generating google-service.json file, check out the complete documentation.
After adding firebase to your account, if you are using UI implementation method, the process of activating notification is complete. But if you are using core or combined implementation, you can override the onNotificationReceived method in TapListener to implement your own push notification style, or you can use TapTalk's default notification like below.
Java
Kotlin
1
TapListener tapListener = new TapListener() {
2
@Override
3
public void onNotificationReceived(TAPMessageModel message) {
4
// Implement your notification here
5
super.onNotificationReceived(message); // Using super will show TapTalk's default notification
6
}
7
};
Copied!
1
var tapListener: TapListener = object : TapListener() {
2
override fun onNotificationReceived(message: TAPMessageModel?) {
3
// Implement your notification here
4
super.onNotificationReceived(message) // Using super will show TapTalk's default notification
5
}
6
}
Copied!
Note: TapTalk will also show its default notification if your do not override the onNotificationReceived method in TapListener
If you are using TapTalk's default chat notification, TapTalk's chat room will be opened when the user taps the push notification from the phone. Note that your application's main activity will not be present when this happens, and the application will exit once the user closes the chat room. To prevent this, you can override the onTaskRootChatRoomClosed method in TapListener to open your application's main activity when the user closes the chat room as shown below.
Java
Kotlin
1
TapListener tapListener = new TapListener() {
2
@Override
3
public void onTaskRootChatRoomClosed(Activity activity) {
4
// Open your application's main activity here
5
Intent intent = new Intent(activity, YourMainActivity.class);
6
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
7
activity.startActivity(intent);
8
}
9
};
Copied!
1
var tapListener: TapListener = object : TapListener() {
2
override fun onTaskRootChatRoomClosed(activity: Activity?) {
3
// Open your application's main activity here
4
val intent = Intent(activity, YourMainActivity::class.java)
5
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
6
activity.startActivity(intent)
7
}
8
}
Copied!

Step 3: Register the FCM Server Key and Sender ID to Taptalk.io Dashboard

1. Go to TapTalk.io Dashboard page, and login to your dashboard. 2. Select Tab Development -> Push Certificates
3. Next, select Add FCM Key
4. Fill in the FCM Server Key and FCM Sender ID field and click Save
5. Finally the FCM Server Key and FCM Sender ID will be saved to your project and will be used for push notifications in your application.
Last modified 10mo ago