简体   繁体   English

“<app> 在使用 Google Play 服务时遇到问题。 请再试一次”,同时使用 Google NearBy Messages API</app>

[英]“<App> is having trouble with Google Play Services. Please try again” while usiing Google NearBy Messages API

While using google NearBy Messages API I am getting error " is having trouble with Google Play Services. Please try again"使用 google NearBy Messages API 时出现错误“Google Play 服务有问题。请重试”

Please guide me for this issue.请指导我解决这个问题。

每当我发布消息时。出现此错误

Below is import for Google Messaging API下面是谷歌消息 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)

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.所有的消息 API 都应该在前台 Activity 中使用,除了带有 PendingIntent 参数的 subscribe 变体。 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 应该在 onStart() 中发布(消息)或订阅(MessageListener),或者响应可见 Activity 中的用户操作,并且您应该始终在 onStop() 中对称地取消发布(消息)或取消订阅(消息监听)。

  • When subcribe, if using activity, it will ask to grant permission to bluetooth, location, microphone, if using service it will not ask订阅时,如果使用activity,它会要求授予蓝牙、位置、麦克风的权限,如果使用服务则不会询问

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.当你在 mainActivity 中订阅时,如果另一个活动出现在顶部(然后 MainActivty 将是 onStop),则会出现一个通知。 Therefore, when subcribe, you must click OK to allow the another activity to be displayed因此,订阅时,您必须单击确定以允许显示另一个活动

This is sample:这是示例:

MainActivity.tk 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 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())
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序在使用Google Play服务时遇到问题。 请再试一次 - App is having trouble with Google Play Services. Please try again 如何解决“应用程序在使用谷歌播放服务时遇到问题。 请再试一次。” - How to resolve “App is having trouble with google play services. please try again.” 应用程序在使用Google Play服务时遇到问题。 如果仍然存在,请联系开发人员以获取帮助 - App is having trouble with Google Play Services. If it persists, please contact the developer for assistance <Your App> Google Play 服务出现问题。请重试。 仅在 4.4.X 操作系统设备上 - <Your App> is having trouble with Google Play services.Please try again. Only on 4.4.X OS devices 更新Google Play服务。 除非您更新Google Play服务,否则此应用无法运行 - Update Google Play services. This app won't run unless you update Google Play services 更新Google Play服务。 除非您更新Google Play服务,否则应用程序将无法运行 - Update Google Play Services. App won't work unless you update google play services Google Play服务附近消息授权对话框未完全显示 - Google Play Services Nearby Messages authorization dialog not fully shown 无法登录。 请检查您的网络连接广告,然后重试,Google Play游戏服务 - failed to sign in . Please check your network connection ad try again , google play game services Google Play服务“无法登录。请检查您的网络连接,然后重试” - Google Play Services “Failed to sign in. Please check your network connection and try again” 在android studio中,BasicSamples项目抱怨“除非您更新Google Play服务,否则此应用将无法运行。” - In android studio, the BasicSamples project complains that “This app won't run unless you update Google Play services.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM