Android 如何为Firestore集合查询正确创建通知?
因此,当在Firestore集合中添加文档时,我成功地实现了通知 这是密码Android 如何为Firestore集合查询正确创建通知?,android,kotlin,notifications,google-cloud-firestore,Android,Kotlin,Notifications,Google Cloud Firestore,因此,当在Firestore集合中添加文档时,我成功地实现了通知 这是密码 snip>code updated below 除了有一个问题外,它工作得很好 每次我关闭应用程序,然后重新打开它,通知就会再次出现。是否有办法使通知在添加新文档后仅显示一次 编辑:我尝试添加时间戳和if条件,但不起作用 val nVoucher = HashMap<String, Any>() nVoucher["timestamp"] = Timestamp.now().seconds val
snip>code updated below
除了有一个问题外,它工作得很好
每次我关闭应用程序,然后重新打开它,通知就会再次出现。是否有办法使通知在添加新文档后仅显示一次
编辑:我尝试添加时间戳和
if
条件,但不起作用
val nVoucher = HashMap<String, Any>()
nVoucher["timestamp"] = Timestamp.now().seconds
val nVoucher=HashMap()
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)
}
db!!。收款(“凭单”)
.whereGreaterThan(“jumlah”,0).addSnapshotListener{documentSnapshots,e->
如果(e!=null){
Log.d(标记“错误:”+e.message)
}
if(documentSnapshots!=null){
documentSnapshots.query
用于(文档快照中的文档。文档更改){
如果(doc.type==DocumentChange.type.ADDED){
跑{
val-nvoucher=doc.document.toObject(detil凭证::class.java)
nvoucher.docID=doc.document.id
凭证!!.add(nvoucher)
val judul=doc.document.get(“judul”).toString()
val gambar=doc.document.get(“gambar”).toString()
val docTime=doc.document.get(“时间戳”)
val timenow=Timestamp.now().seconds
if(timenow==docTime){
remoteViews!!.setImageViewResource(R.id.remoteview\u通知\u图像,android.R.drawable.ic\u对话框\u信息)
remoteview!!.setTextViewText(R.id.remoteview\u通知\u标题,“凭证Baru”)
remoteViews!!.setTextViewText(R.id.remoteview\u通知\u短消息,judul)
val notifID=101
val channelID=“com.sample.notification”
val notification=notification.Builder(上下文,channelID)
.setContentTitle(“凭证Baru”)
.setContentText(judul)
.setSmallIcon(android.R.drawable.ic\u对话框\u信息)
.setChannelId(channelID)
.setCustomContentView(远程视图)
.setContentIntent(pIntent)
.build()
val notificationTarget:notificationTarget=notificationTarget(
上下文
R.id.remoteview\u通知\u图像,
远程视图,
通知,
通报
)
滑翔(this@Home.context!!.applicationContext).asBitmap().load(gambar).into(notificationTarget)
notificationManager!!.notify(notifID,notification)}
vouchaerAdapter!!.notifyDataSetChanged()
}
}
}
}
}
通知管理器=this@Home.activity!!.getSystemService(Context.NOTIFICATION\u服务)作为NotificationManager
createNotificationChannel(
“com.sample.notification”,
“代金券巴鲁”,
“优惠券促销”
)
private fun createNotificationChannel(id:String,judul:String,detil:String){
val重要性:Int=NotificationManager.importance\u低
val通道=通知通道(id、judul、重要性)
channel.description=detil
通道.启用灯(真)
channel.lightColor=Color.RED
通道启用振动(真)
channel.vibrationPattern=longArrayOf(100200300400500400300200400)
notificationManager!!.createNotificationChannel(频道)
}
再次运行应用程序时,您正在将侦听器附加到集合/查询。每次这样做时,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);
}
val timestamp=GetTimeStampFromSharedReferences();
val now=时间戳.now().seconds
用于(文档快照中的文档。文档更改){
val docTimestamp=doc.document.get(“时间戳”)
if(timestamp==null | | timestamp
Hi,@Frank van poffelen我尝试使用时间戳,但它不起作用,请检查上面的更新,我更新了我的答案,以包含这种处理类型的伪代码。