"<App> is having trouble with Google Play Services. Please try again" while usiing Google NearBy Messages API

443 Views Asked by At

While using google NearBy Messages API I am getting error " is having trouble with Google Play Services. Please try again"

Please guide me for this issue.

Whnever i am publishing message. this error appears

Below is import for Google Messaging API

    implementation 'com.google.android.gms:play-services-nearby:17.0.0'

Here is how I am subscribing using code

    val options = SubscribeOptions.Builder()
        .setStrategy(Strategy.BLE_ONLY)
        .build()
    Nearby.getMessagesClient(
        this, MessagesOptions.Builder()
            .setPermissions(NearbyPermissions.BLE)
            .build())
    Nearby.getMessagesClient(this).subscribe(getPendingIntent(), options)
1

There are 1 best solutions below

0
On

I resolved it.

All of the Messages APIs should be used from a foreground Activity, with the exception of the variants of subscribe that take a PendingIntent parameter. Your Activity should publish(Message) or subscribe(MessageListener) either in onStart() or in response to a user action in a visible Activity, and you should always symmetrically unpublish(Message) or unsubscribe(MessageListener) in onStop().

  • When subcribe, if using activity, it will ask to grant permission to bluetooth, location, microphone, if using service it will not ask

So if you use the service, you must combine using the activity.

  • When you subscribe in mainActivity, if another activity appears on top (then MainActivty will be onStop), a notification will appear. Therefore, when subcribe, you must click OK to allow the another activity to be displayed

This is sample:

MainActivity.tk

private val mMessageListener: MessageListener = object : MessageListener() {
        override fun onFound(message: Message) {
            Log.d(TAG, "onFound message:"+ String(message.content))
        }

        override fun onLost(message: Message) {
            Log.d(TAG, "Lost sight of message: " + String(message.content))
        }
    }
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val sharedPref: SharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE)
    val isFirstTime = sharedPref.getBoolean("FIRST_TIME", true)
    if(isFirstTime) {
            Nearby.getMessagesClient(this).subscribe(mMessageListener).addOnCompleteListener(this, OnCompleteListener {
                requestPermissionFirstTime()
            }).addOnCanceledListener(this, OnCanceledListener {
                requestPermissionFirstTime()
            })
        } else {
            requestPermissionCapture()
            checkPermissionAccessibility()
            startService(Intent(this, NearbyMessageService::class.java))
        }

}
private fun requestPermissionFirstTime() {
        val sharedPref: SharedPreferences = getSharedPreferences(Utils.IAMHERE_PREF, Context.MODE_PRIVATE)
        val editor = sharedPref.edit()
        editor.putBoolean("FIRST_TIME", false)
        editor.apply()
        Nearby.getMessagesClient(this).unsubscribe(mMessageListener)
        requestPermissionCapture()
        checkPermissionAccessibility()
    }

NearbyMessageService.tk

class NearbyMessageService: IntentService("NearbyMessageService") {
private val mMessageListener: MessageListener = object : MessageListener() {
        override fun onFound(message: Message) {
            Log.d(TAG, "onFound message:"+ String(message.content))
        }

        override fun onLost(message: Message) {
            Log.d(TAG, "Lost sight of message: " + String(message.content))
        }
    }
    override fun onCreate() {
        super.onCreate()
        startForeground()
        Nearby.getMessagesClient(this).subscribe(mMessageListener)
    }

private fun startForeground() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "002"
            val channelName = "Nearby Service Channel"
            val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
            channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
            val notification: Notification = Notification.Builder(applicationContext, channelId)
                .setOngoing(true)
                .setCategory(Notification.CATEGORY_SERVICE)
                .setContentTitle(getString(R.string.app_name))
                .build()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                startForeground(Utils.NOTICATION_ID_NEARBY, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
            } else {
                startForeground(Utils.NOTICATION_ID_NEARBY, notification)
            }
        } else {
            startForeground(Utils.NOTICATION_ID_NEARBY, Notification())
        }
    }

}