因此,我成功地实现了一个通知,当一个文档添加到Fi还原集合中时,
这是密码
snip>code updated below
它工作得很好,除了有一个问题,
每次我关闭应用程序,然后重新打开它,通知再次出现。在添加了新文档之后,是否有任何方法使通知只出现一次?
编辑:我尝试过添加时间戳和if
条件,但是它不起作用,
val nVoucher = HashMap<String, Any>()
nVoucher["timestamp"] = Timestamp.now().seconds
新码
db!!.collection("voucher")
.whereGreaterThan("jumlah", 0).addSnapshotListener { documentSnapshots, e ->
if (e != null) {
Log.d(TAG, "Error :" + e.message)
}
if (documentSnapshots != null) {
documentSnapshots.query
for (doc in documentSnapshots.documentChanges) {
if (doc.type == DocumentChange.Type.ADDED) {
run {
val nvoucher = doc.document.toObject<DetilVoucher>(DetilVoucher::class.java)
nvoucher.docID = doc.document.id
voucher!!.add(nvoucher)
val judul = doc.document.get("judul").toString()
val gambar = doc.document.get("gambar").toString()
val docTime = doc.document.get("timestamp")
val timenow = Timestamp.now().seconds
if (timenow == docTime) {
remoteViews!!.setImageViewResource(R.id.remoteview_notification_image, android.R.drawable.ic_dialog_info)
remoteViews!!.setTextViewText(R.id.remoteview_notification_headline, "Voucher Baru")
remoteViews!!.setTextViewText(R.id.remoteview_notification_short_message, judul)
val notifID = 101
val channelID = "com.sample.notification"
val notification = Notification.Builder(context, channelID)
.setContentTitle("Voucher Baru")
.setContentText(judul)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setChannelId(channelID)
.setCustomContentView(remoteViews)
.setContentIntent(pIntent)
.build()
val notificationTarget: NotificationTarget = NotificationTarget(
context,
R.id.remoteview_notification_image,
remoteViews,
notification,
notifID
)
Glide.with(this@Home.context!!.applicationContext).asBitmap().load(gambar).into(notificationTarget)
notificationManager!!.notify(notifID, notification)}
vouchaerAdapter!!.notifyDataSetChanged()
}
}
}
}
}
notificationManager = this@Home.activity!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createNotificationChannel(
"com.sample.notification",
"Voucher Baru",
"Voucher Promo"
)
private fun createNotificationChannel(id: String, judul: String, detil: String) {
val importance : Int = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(id, judul, importance)
channel.description = detil
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
notificationManager!!.createNotificationChannel(channel)
}
发布于 2019-08-18 19:48:31
当您再次运行应用程序时,您将重新将侦听器附加到集合/查询。每次这样做,docChanges
都会为集合/查询中存在的每个文档使用doc.type == DocumentChange.Type.ADDED
。
如果你不想对你以前做过的文件采取行动,你必须自己追踪。通常,您可以通过在本地存储中存储一些数据,或者在用户需要跨设备工作时将其存储在数据库中。
最简单的场景是,如果文档有某种形式的创建/修改日期,那么您可以在上次处理文档时存储时间戳。
如果没有时间戳(而且不能添加时间戳),则必须跟踪在单个文档级别上处理过的文档。虽然这是绝对可能的,但这是一个更多的家务事,所以我肯定会尝试基于时间戳的方法首先。
在更新的代码中,您将文档中的时间戳与当前时间进行比较,这不太可能是真的。你会想要这样的:
val timestamp = getTimestampFromSharedPreferences();
val now = Timestamp.now().seconds
for (doc in documentSnapshots.documentChanges) {
val docTimestamp = doc.document.get("timestamp")
if (timestamp == null || timestamp < docTimestamp) {
...
}
setTimestampToSharedPreferences(now);
}
https://stackoverflow.com/questions/57550201
复制相似问题