我开始学习信标,并且最近开始在我的Android应用中使用它们。但是我无法解决给我的问题: 1.每当我的Android Oreo设备进入信标区域时,都会显示推送通知。 2.每当我的Android Oreo设备退出信标区域时,都会显示推送通知
条件:
这是MyApplication类的代码
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.util.Log
import com.estimote.coresdk.observation.region.beacon.BeaconRegion
import com.estimote.coresdk.recognition.packets.Beacon
import com.estimote.coresdk.service.BeaconManager
import java.util.*
class MyApplication : Application() {
private var beaconManager: BeaconManager? = null
val TAG = javaClass.name
override fun onCreate() {
super.onCreate()
beaconManager = BeaconManager(applicationContext)
beaconManager!!.setForegroundScanPeriod(1000, 0)
beaconManager!!.setMonitoringListener(object : BeaconManager.BeaconMonitoringListener {
override fun onExitedRegion(beaconRegion: BeaconRegion?) {
showNotif("Exit", "Bye bye")
Log.e(TAG, "Exit range")
}
override fun onEnteredRegion(beaconRegion: BeaconRegion?, beacons: MutableList<Beacon>?) {
showNotif("Entry", "Hey there")
Log.e(TAG, "Enter range")
}
})
beaconManager!!.connect {
beaconManager!!.startMonitoring(
BeaconRegion(
"monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
1,
1
)
)
}
}
fun showNotif(title: String, text: 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 = Notification.Builder(this, "1")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.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_launcher_background)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build()
//notification.defaults = notification.defaults or Notification.DEFAULT_SOUND
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
}
}
}
此代码在版本低于Oreo的设备中运行良好。但是它不满足Oreo中的所有上述条件。 我尝试使用监视,范围,bootStrapNotifier等。但是每个方法都只能解决上述几种情况,而忽略了其余的情况。 请有人帮我提供符合以上所有条件的代码
预先感谢:)