Saif7
Tera Contributor

find_real_file.png

 

Hello Everyone 😉

Is it possible to make an integration between ServiceNow & FCM- Firebase Cloud Messaging? If you are working on a Native Android app or IOS app and thinking about this question, here is the answer(After a long troubles).

What is the benefit of this integration? Suppose that a requested item need an approval from the department manager, you can automate the mobile notifications! so once an approval is required, send a notification to the department manager, or once an incident state changes from in-progress to on-hold, Simply, you can build your conditions as you need, the great thing here that you are dealing with a NATIVE mobile application! (Someone who has been working on Android development will understand what I mean).

  • When the requester categorized as a VIP User, You can send a specific notification sound through the JSON payload.
  • The user can interact with the mobile notification(taking an action by clicking on a specific button in the notification bar, for example: managers can approve/reject expenses by click on approve button).
  • You can send a photo/screenshot as a part of the JSON payload.

 

Key Steps

 

Outside the scope of Service Now

 

  1. Create firebase account >> https://firebase.google.com.
  2. Prepare your project >> whether it is Android or IOS.
  3. Make your project connected to firebase console.
  4. Create your FMC listener function inside your service class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification().getBody() != null) {
            Log.e("Message Content :", "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }
}

 

Within the scope of ServiceNow

 

In the application navigator, search about push application under system notification, then create new record, the expected input here, information about your application name, feedback, and push, etc.

IMPORTANT NOTE: I don't know why ServiceNow doesn't show the Push REST Message & Feedback REST Message fields automatically, you have to show the fields using SN Utils.

find_real_file.png

in google section, you should insert the API Key for your Firebase Cloud Messaging,

How you can get the API Key?

 

That's simple, go to your firebase console, then project settings, click on cloud messaging, copy the server key.
Open the Firebase Cloud Messaging Send REST API, Add the following elements to the header

 

find_real_file.png

 

IMPORTANT NOTE: Authorization is a key that refer the to Server Key(API) for your firebase cloud messaging, Firebase never updates this code, it is a fixed Key for your project.

Open the project we just created, At the bottom of the page you can see related list(Push Notification Message Contents), create on new and past the code below, this code will generate the notification, you can customize yours data as you want, add or remove objects, also current: access properties of the record that triggered this push notification, such as the fields on an Incident record. message: access contents of the message that will be sent as the body of the push notification. attributes: access attributes defined for this push notification, I prefer to write gs.warn or gs.log and print your object to track it and make the debugging easier 

NOTE: TO refers to the person who will receive the alert, this token is a unique identifier for each user, I noticed that the ServiceNow documentation doesn't talk about the importance of this table(sys_push_notif_app_install)! This is what makes the integration unsuccessful for most developers, each user login to your application, should registered in the mentioned table, this is very important, you can read about this table: click here, I wrote the token as a fixed value JUST FOR DEMO.

    var json = {};

    // build JSON sent by push here

    json = {

        "to": "fyp9hcB-SqOoFvUORx0FEB:APA91bEuH8HXS47pgy6S_ewkhqTQLGYVq6zoamgkp05GfEFbL1SQpLd51BNr81VSMxjwDuWsBC5Q3DtKGxpyqmuqHvYSZqjMFsRjrt5vzyJetkClg0_meBTGLPkrhVqZM2fGWS18jYcA",
        "notification": {
            "body": "Incident Updated",
            "title": "Incident Number:" + current.number + "has been completed"
        }
    };

    return json;

 

How can I get the user token?

 

I think you can get a user's token with the following code:

var currentUser = current.opened_by; // or assigned_to( your target receiver)
var userToken = null;

var getUserToken = new GlideRecord('sys_push_notif_app_install');
getUserToken.addQuery('sys_user_id',currentUser);
getUserToken.addQuery('active',true);
getUserToken.query();
if(getUserToken.next()){
userToken = getUserToken.token;
}

 

Search about Push Notification under system notification in the application navigator, create new and select your target table, conidiations, and make sure that you select PUSH Notification only, also you need to select your application in Push Messages 

 

find_real_file.png

I set a condition for the alert, that it runs when an incident state changes, So it can be summarized that when the incident state change to closed, an notification will be sent to the application instance defined by the token that we specified before.

How can I monitor notifications? search about Push Notifications under System logs in the application Navigator, you can track all the notifications and the status, this is the end of the steps and there is nothing else to do.

find_real_file.png

 

The Payload Issue

 

I Know that many developers are trying to change the Payload(Payload field in sys_push_notification table) through Business rule or something, Because ServiceNow forces you to have the root of the payload start with "Data" and this affects the basic functions of FMC, In my experience, the payload should not be changed in any way because the notification will not send, no matter how hard you try in this regard, you will get a "failure" status, I found an article that talks about this: here, this issue does not affect the sending of the notification in any case, but it will be difficult to deal with JSON Arrays in your application.

 

NOTE: in sys_push_notification table, you must create a record that contain the TOKEN for the registered user

 

Postman to test your payload

 

Communication between ServiceNow and your application is based on API: https://fcm.googleapis.com/fcm/send  - POST

Send the Authorization key in the header & Content-Type:application/json, then in the row section write your object, you should receive a response that show status for your message, its working also for real-time testing in android/XCode

{
    "multicast_id": 4039920926381147238,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1630951875768865%fc1ec892fc1ec892"
        }
    ]
}

 

Customizations

 

If you need to set a specific notification sound or icon, I think you will need to add sound key to your JSON code, just you need to parse these Keys in your android/iOS app and start to build your logical procedures.

 {
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",

    "notification" : {
      "body" : "welcome!",
      "title" : "Welcome to Istanbul Airport",
      "icon" : "myicon",
      "sound" : "mySound"
    }
  }

 

Thank you.

Version history
Last update:
‎09-06-2021 01:37 PM
Updated by: