android - 使用Google NearBy Messages API时“<App> is having trouble with Google Play Services. Please try again”

标签 android kotlin google-nearby google-nearby-messages

使用google NearBy Messages API时,出现错误“Google Play服务出现问题。请重试”

请指导我解决这个问题。

Whnever i am publishing message. this error appears

以下是Google Messaging API的导入

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

这是我使用代码进行订阅的方式
    val options = SubscribeOptions.Builder()
        .setStrategy(Strategy.BLE_ONLY)
        .build()
    Nearby.getMessagesClient(
        this, MessagesOptions.Builder()
            .setPermissions(NearbyPermissions.BLE)
            .build())
    Nearby.getMessagesClient(this).subscribe(getPendingIntent(), options)

最佳答案

我解决了

  • 附近建议使用 Activity , Activity 时,该功能将更好地工作(https://developers.google.com/android/reference/com/google/android/gms/nearby/messages/MessagesClient#subscribe(android.app.PendingIntent,%20com.google.android.gms.nearby.messages.SubscribeOptions))

  • 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().


  • 订阅时,如果使用 Activity ,它将要求授予对蓝牙,位置,麦克风的许可,如果使用服务,它将不询问

  • 因此,如果您使用该服务,则必须使用该 Activity 进行合并。
  • 如果您在mainActivity中订阅,则在上订阅(如果另一个 Activity 出现在顶部(然后MainActivty将在onStop上)),则会出现一条通知。
    因此,订阅时,必须单击“确定”以允许显示另一个 Activity

  • 这是示例:
    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()
        }
    
    附近的消息服务.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())
            }
        }
    
    }
    

    关于android - 使用Google NearBy Messages API时“<App> is having trouble with Google Play Services. Please try again”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61325728/

    相关文章:

    java - 读取 PEM 证书链中的数据

    Kotlin @FunctionalInterface 使用多种抽象方法进行编译

    android - 接近信标 API : Android and Nearby

    java - 订阅时出现 Android Nearby API NullPointerException

    android - 如何画一个透明的圆圈?

    android - 配置仍然不正确。您要再次编辑吗?在运行应用程序时在android studio中

    kotlin - 如何从Kotlin中相同大小的MutableList <MutableList <Boolean >>创建MutableList <MutableList <Int >>

    jenkins - 在根项目 'assembleDebug'中找不到任务 'my-project'

    iOS 和 Google Nearby API : how to publish and subscribe in the right way?

    android - Audiorecord.read() 值的含义