NowPushService class - Android

  • Release version: Xanadu
  • Updated August 1, 2024
  • 2 minutes to read
  • The NowPushService class provides functions that enable interaction with the Push Notification service.

    Table 1. Properties
    Name Description
    configuration Service configuration to associate with the service.

    Data type: NowServiceConfiguration

    NowPushService - handlePush(remoteMessage: RemoteMessage, successCallback: Consumer<NowPushPayload>, errorCallback: Consumer<Throwable>)

    Processes a push notification request.

    Note:
    Currently the only implemented push notification type is NowPushVirtualAgent. Any other passed push notification type returns a NotSupportedPushError object.
    Table 2. Parameters
    Name Type Description
    remote Message Remote Message Notification as received by com.google.firebase.messaging.FirebaseMessagingService.onMessageReceived. For additional information, see Receive messages in an Android app.
    success Callback Consumer <NowPushPayload> Callback to return the processed NowPushPayload to.
    error Callback Consumer <Throwable> Callback to return the thrown error to. If the error NotSupportedPushError is thrown, the notification type is not supported by the mobile SDK, and must be processed outside of the mobile SDK framework.
    Table 3. Returns
    Type Description
    None

    This example shows how to override onMessageReceived() and pass the RemoteMessage through to the NowPushService. If the NowPushService recognizes the notification type, it processes the request and returns the notification object for the application to handle. Otherwise it throws the NotSupportedPushError.

    override fun onMessageReceived(remodeeMessage: RemoteMessage){
      pushService.handlePush(remoteMessage, { push ->
        when (push) {
          is NowPushVirtualAgent → handleVirtualAgentPush(push)
        }, { error ->
            Log.e(TAG, "Unknown push", error)
            handleAppPushNotification(remoteMessage)
        })
    }

    NowPushService - registerPushToken(pushToken: String, pushApp: String, successCallback: Runnable, errorCallback: Consumer<Throwable>)

    Registers a unique Firebase token with the ServiceNow instance used to identify the push notifications for the current Android device and specified application.

    In order for the ServiceNow instance to generate notifications Android device to receive notifications from the application, this token must be registered.

    Table 4. Parameters
    Name Type Description
    pushToken String Firebase token retrieved by either com.google.firebase.messaging.FirebaseMessaging.getToken or com.google.firebase.messaging.FirebaseMessagingService.onNewToken.
    pushApp String Name of the push application as specified in the Push Application table on the associated ServiceNow instance.
    success Callback Runnable Callback executed when the token registration is successful.
    errorCallback Consumer <Throwable> Callback executed when the token registration fails.
    Table 5. Returns
    Type Description
    None

    This example registers a push token for the current Android device and the application "PushAppName".

    FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
      val token = task.result
    
      if (!task.isSuccessful || token == null) {
        throw Exception("Unable to fetch token"))
      }
    
      pushService.registerPushToken(token, "PushAppName", {
        Log.v(TAG, "Successfully registered push token")
      }, { e ->
        Log.e(TAG, "Error registering push", e)
      })
    }

    NowPushService - unregisterPushToken(pushToken: String, pushApp: String, successCallback: Runnable, errorCallback: Consumer<Throwable>)

    Unregisters the specified Firebase push token with the associated ServiceNow instance.

    Table 6. Parameters
    Name Type Description
    pushToken String Firebase token to unregister. Retrieved by either com.google.firebase.messaging.FirebaseMessaging.getToken or com.google.firebase.messaging.FirebaseMessagingService.onNewToken.
    pushApp String Name of the push application associated with the token to unregister. This information is stored in the Push Application table on the associated ServiceNow instance.
    success Callback Runnable Callback executed when the token unregistration is successful.
    erro rCallback Consumer <Throwable> Callback executed when the token unregistration fails.
    Table 7. Returns
    Type Description
    None

    This code example shows how to unregister a push token such as when the user logs out of the application.

    pushService.unregisterPushtoken(token, "PushAppName", {
      Log.v(TAG, "Successfully unregistered push token")
    },  { e ->
      Log.e(TAG, "Error unregistering push", e)
    })