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

时间:2018-10-08 10:00:12

标签: android kotlin push-notification beacon eddystone

借助this链接,当我的设备进入信标范围时,我可以显示通知。当设备移出信标范围时,我还想显示一条通知。我该在哪提?

这是我的代码

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 个答案:

答案 0 :(得分:0)

一些提示:

  1. 使用信标监视,而不是信标范围。监视API在信标首次出现时给您一个回调(didEnterRegion),在信标消失时给您一个回调(didExitRegion)。您可以将逻辑移到这些回调中。请参阅“信标出现在区域here中时得到通知。

  2. 当前,代码通过检查服务UUID并手动键入代码来检查它是否是Eddystone信标。不要这样相反,请清除除Eddystone以外的所有BeaconParsers,以便您知道何时获得回调。