How to add push notifications to BLiP Chat Android July 04, 2024 14:02 Updated Index: Setting up FCM in the application Registering users' FCM tokens in BLiP Chat Sending the private key file from your service account Testing the integration In some specific scenarios, it may be necessary to add push notifications to Android applications that embed BLiP Chat. Through Push Notification, it's possible to notify the user about unread messages. For this, specific configurations are required between the application (which embeds BLiP Chat), FCM (Firebase Cloud Messaging), and BLiP. Below are the steps required to perform these configurations. 1 - Setting up FCM in the application Add Firebase to your Android project, if you haven't done so already.Set up a Firebase Cloud Messaging client app in your Android project. 2 - Registering users' FCM tokens in BLiP Chat For BLiP Chat to send notifications, it's necessary to register the user's token on the BLiP server. This information allows BLiP to determine which Google FCM credential belongs to a BLiP Chat user. Important: The integration with FCM was initially validated using our native Android library in native projects only. Uses with Flutter or integrations in WebView have not yet been fully tested and documented. The user's token should be captured using the onTokenRefresh method of your class that inherits from FirebaseInstanceIdService. @Overridepublic void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); // Store token to send later when connecting to blip chat. storeTokenUsingAnyMethod(refreshedToken);} The stored token must be passed to BLiP Chat when the BlipClient is loaded. To do this, it's necessary to pass the user's FCM token concatenated with @firebase.gw.msging.net in the extras property of the user's account. The value should be set as an item in the extras property of the Account. The extras property is a Map<String,String>, the item should have the key '#inbox.forwardTo' and the value "{storedToken}@firebase.gw.msging.net". See the example below: AuthConfig authConfig = new AuthConfig(AuthType.Dev, "userIdentifier","userPassword");Map<String, String> extras = new HashMap<>();String fcmUserToken = "stored_user_token";extras.put("#inbox.forwardTo", String.format("%s@firebase.gw.msging.net", fcmUserToken));Account account = new Account();account.setFullName("User Name");account.setEmail("user@gmail.com");account.setEncryptMessageContent(true);account.setExtras(extras);BlipOptions blipOptions = new BlipOptions();blipOptions.setAuthConfig(authConfig);blipOptions.setAccount(account);BlipClient.openBlipThread(MainActivity.this, BuildConfig.APPKEY, blipOptions); 3 - Sending the private key file from your service account In addition to steps 1 and 2, it's necessary to send BLiP a file containing the private keys of your FCM project. To generate the file with these configurations, follow the steps below: In the Firebase console, go to Settings > Service accounts. Click on Generate new private key and confirm by clicking Generate key. Securely store the JSON file containing the key. Send an email (without attaching the file) to blip@blip.ai with the subject Integration FCM. The BLiP team will respond with instructions on where to store the private key file. Note: Do not attach the private key file to the email. After completing the steps above, your application will be able to receive messages. Nota: Currently, to complete this process, open a support ticket (https://support.blip.ai/), attach the generated JSON file, and request further instructions. Testing the integration Create a class that inherits from FirebaseMessagingService to consume pushes sent by BLiP using the method below: @Overridepublic void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); // Handle message within 10 seconds handleNow(); }} The return of the method remoteMessage.getData() is a Map<String, String> with 3 items, which represent a message in the protocol used by BLiP Chat. ‘id’ = message identifier‘content’ = json of the message content.‘type’ = message type. Message contents and types are exemplified here For more information, visit the discussion on the subject at our community or videos on our channel. 😃 Related articles How to add a bot to an Android application using Blip Chat Features of the Blip Chat Widget How to add a bot to an iOS application using BliP Chat How to add a bot to a website using Blip Chat How to Send SMS via API