UIKit Android Push Notifications no device token

Hi all, I’m using UIKit v2.2.2 with Android, and I’m trying to send push notifications, but it doesn’t work because my users don’t have a device token. The problem is that I have another external library that uses Firebase and Huawei push notifications. Right now, I’ve done the following steps:

  1. Created FCM and HMS services and added in the manifest
  2. Override onNewToken and called SendBird.registerPushTokenForCurrentUser
  3. Override onMessageReceived and handled notifications.

Of course, I’ve already registered the server keys and placed services’ JSON file in the app.

I’m not using SendBirdPushHelper.registerPushHandler and I don’t know if it’s mandatory for push, could you help me to understand the proper way to implement that?

Thanks

I think the issue is you use other library that uses Firebase and Huawei.
Our Chat SDK should handle all messages from Firebase and Huawei. Is it possible unregister other library? How do you implement other library?

Like Sendibird, the other library has some methods called in onNewToken and onMessageReceived also it has an init method called in App. But the problem is that I can’t obtain a device token for my users. I tried a workaround adding SendBirdPushHelper.registerPushHandler(FirebaseMessagingServiceHandler()) in App where FirebaseMessagingServiceHandler extends SendBirdPushHandler and now my users have token, but how can I do the same with Huawei?

Hi @Barros

I think you don’t implement

  1. Created FCM and HMS services and added in the manifest

Our Sendbird Chat SDK manifest already define services. Then, you can receive all messages from

SendBirdPushHandler

Hi @Doo_Rim

I’m trying to not implement FCM and HMS services as you suggested, but the problem is still there I can’t obtain a token for the users, if you look the screenshot below, I can’t send a test push

Schermata 2022-01-03 alle 10.49.11

The problem, it could be related to the other library because it uses FirebaseMessagingService and HmsMessageService too, for that I tried to create two services (FCM and HMS) to handle both logic of Sendbird and the other library. But it works only, as I said above, if I create a FirebaseMessagingServiceHandler that extends SendBirdPushHandler and register with registerPushHandle in App, and of course I can’t do the same for Huawei.

Do you have any idea how to fix that? Sorry it seems a tough question, thank you for your time.

Did you register server key to your dashboard?

  • Did you implement registerPushToken method?

Did you register server key to your dashboard?

Yes, I had registered the server keys, in fact with FirebaseMessagingServiceHandler workaround it works normally, and I have users with token who can receive chat messages.

Did you implement registerPushToken method?

Yes, I created a FirebaseMessagingService, added in the manifest that implements registerPushTokenForCurrentUser

<service
     android:name="com.example.FirebaseMessagingService"
     android:exported="false">
       <intent-filter>
           <action android:name="com.google.firebase.MESSAGING_EVENT" />
       </intent-filter>
</service>
class FirebaseMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        if (TextUtils.isEmpty(token)) {
            return
        }

        SendBird.registerPushTokenForCurrentUser(token) { pushTokenRegistrationStatus, error ->
            if (error != null) {
                Timber.e(error)
            }

            if (pushTokenRegistrationStatus == PushTokenRegistrationStatus.PENDING) {
                // A token registration is pending.
                // Retry the registration after a connection has been successfully established.
            }
        }
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) { ... }
}

What I understood is

  1. You implement both FirebaseMessagingServiceHandler that extends SendBirdPushHandler and
    FirebaseMessagingService that extends FirebaseMessagingService.
  2. You can receive token from FirebaseMessagingService that extends FirebaseMessagingService. However, registerPushTokenForCurrentUser doesn’t work well. Right?

If above information is right, Could you give me a error log for registerPushTokenForCurrentUser?

I found the problem, in my implementation, SendBirdUIKit.init() isn’t called at the startup because I need some information from our backend to populate UserInfo. So, when I receive the token from FirebaseMessagingService that extends FirebaseMessagingService I can’t invoke successfully registerPushTokenForCurrentUser because SendBirdUIKit isn’t initialize. I’ve implemented the following:

  1. Removed FirebaseMessagingServiceHandler that extends SendBirdPushHandler
  2. When user info are available called SendBirdUIKit.init()
  3. In onInitSucceed of InitResultHandler called SendBirdUIKit.connect
  4. Retrieved FCM or HMS push token
  5. Called registerPushTokenForCurrentUser

With that, I have users with push token that receive notifications. But now I have another issue, I don’t receive push when app is in foreground, should I call FirebaseMessagingServiceHandler with registerPushHandler inside SendBirdUIKit.connect? I already selected Send to devices both offline and online option.

Please refer to this document. (Multi-device support )

In the Step4, You should use SendBirdPushHelper.registerPushHandler() with the class inheriting SendBirdPushHandler. SendBirdPushHandlerhasalwaysReceiveMessage(): Booleanmethod. You should override this method andreturn true` (default is false). Then, you can receive push notification foreground.

Now it works. Thank you for your support.

1 Like