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.

dependencies {
    classpath 'com.google.gms:google-services:$gms_version'
}

Apply google play service plugin and implement firebase messaging dependency to your app-level build.gradle

apply plugin: 'com.google.gms.google-services'

dependencies {
   implementation 'com.google.firebase:firebase-messaging:$firebase_version'
}

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.

public class YourFirebaseMessagingService extends FirebaseMessagingService {

  @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (TapTalk.isTapTalkNotification(remoteMessage)) {
            TapTalk.handleTapTalkPushNotification(remoteMessage);
        } else {
            // Your own notification here 
        }
    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        TapTalk.saveFirebaseToken(s);
        TapTalk.updateFirebaseTokenToServer(new TapCommonListener() {
            });
    }
}

Otherwise, if you don't handle your own app notification, you can add Taptalk default Firebase Service to your Manifest file.AndroidManifest.xml

<service android:name="io.taptalk.TapTalk.Firebase.TapFirebaseMessagingService">
     <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
     </intent-filter>
</service>

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.

TapListener tapListener = new TapListener() {
    @Override
    public void onNotificationReceived(TAPMessageModel message) {
        // Implement your notification here
        super.onNotificationReceived(message); // Using super will show TapTalk's default notification
    }
};

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.

TapListener tapListener = new TapListener() {
    @Override
    public void onTaskRootChatRoomClosed(Activity activity) {
        // Open your application's main activity here
        Intent intent = new Intent(activity, YourMainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
    }
};

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 updated