Support Topics Documentation Slack YouTube Blog

How to send push notification to specific device in android

Hello.
I want to send a notification to a specific device in android but I get an error in android.
i used this code:

       val deliveryOptions = DeliveryOptions()
                        deliveryOptions.pushSinglecast = listOf("0244cdfc3024c25a")

                        val publishOptions = PublishOptions()
                        publishOptions.putHeader("android-ticker-text", "You just got a private push notification!")
                        publishOptions.putHeader("android-content-title", "This is a notification title")
                        publishOptions.putHeader("android-content-text", "Push Notifications are cool")

                        val status = Backendless.Messaging.publish(
                            "this is a private message!",
                            publishOptions,
                            deliveryOptions
                        )

I get an error in Val status line

Hello Saeed Noshadi,

What error do you get?

Regards,
Olha

this error:
BackendlessException{ code: ‘Internal client exception’, message: 'android.os.NetworkOnMainThreadException

The error you’re getting doesn’t come from Backendless.
Please check this topic: https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception

Regards,
Olha

thank you very much Oiha.
I changed the android kotlin code to this and modified it:

 val deliveryOptions = DeliveryOptions()
                        deliveryOptions.pushSinglecast = listOf("0244cdfc3024c25a")
                        val publishOptions = PublishOptions()
                        publishOptions.putHeader("android-ticker-text", "You just got a private push notification!")
                        publishOptions.putHeader("android-content-title", "This is a notification title")
                        publishOptions.putHeader("android-content-text", "Push Notifications are cool")

                      Backendless.Messaging.publish(
                            "this is a private message!", publishOptions, deliveryOptions, object : AsyncCallback<MessageStatus>{
                              override fun handleFault(fault: BackendlessFault?) {

                                  Timber.e(fault?.message.toString())
                              }

                              override fun handleResponse(response: MessageStatus?) {
                                  Timber.e(response?.status.toString())
                              }

                          })

Is it working fine now?

yes.i should implement object: AsyncCallback for getting responses