当设备超出信标范围时显示通知

Display notification when device is out of beacon range

this link 的帮助下,我能够在我的设备进入信标范围时显示通知。我还想在设备移出信标范围时显示通知。我应该在哪里提到它?

这是我的代码

override fun didRangeBeaconsInRegion(p0: MutableCollection<Beacon>?, p1: Region?) {

        for (beacon in p0!!) {
            if (beacon.getServiceUuid() == 0xfeaa && beacon.getBeaconTypeCode() == 0x00) {
                // This is a Eddystone-UID frame
                val namespaceId = beacon.getId1()
                val instanceId = beacon.getId2()
                Log.d("RangingActivity", "I see a beacon transmitting namespace id: " + namespaceId +
                        " and instance id: " + instanceId +
                        " approximately " + beacon.getDistance() + " meters away.")
                runOnUiThread {
                    showNotification("Entry", "Hello world")
                }
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    fun showNotification(title: String, message: String) {
        val notifyIntent = Intent(this, MainActivity::class.java)
        notifyIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
        val pendingIntent: PendingIntent = PendingIntent.getActivities(
                this,
                0,
                arrayOf(notifyIntent),
                PendingIntent.FLAG_UPDATE_CURRENT
        )

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val mChannel = NotificationChannel("1", "Notify Channel", NotificationManager.IMPORTANCE_HIGH)
            val notification = NotificationCompat.Builder(this, "1")
                    .setSmallIcon(R.drawable.ic_location_arrow)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setChannelId("1")
                    .build()
            notification.defaults = notification.defaults or Notification.DEFAULT_SOUND
            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(mChannel)
            notificationManager.notify(1, notification)
            startForegroundService(notifyIntent)
        } else {
            val notification = NotificationCompat.Builder(this, "1")
                    .setSmallIcon(R.drawable.ic_location_arrow)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setChannelId("1")
                    .build()
            notification.defaults = notification.defaults or Notification.DEFAULT_SOUND
            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(1, notification)
        }
    }

此外,当我在信标范围内时,我会不断收到通知。一旦我在范围内,我希望我的通知只显示一次。

几点提示:

  1. 使用信标监控,而不是信标测距。监控 API 会在信标首次出现时(didEnterRegion)和它消失时(didExitRegion)为您提供一个回调。您可以将您的逻辑移动到这些回调中。请参阅“当信标出现在 here 区域时收到通知。

  2. 目前,代码通过手动检查服务 UUID 和类型代码来检查它是否是 Eddystone 信标。不要这样做。相反,清除除 Eddystone 以外的所有 BeaconParsers,以便您知道何时收到回调。