我想在PendingIntent中执行对Firebase Firestore的异步调用:
MyService.kt
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
/** Intent */
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val action = NotificationCompat.Action(R.drawable.cancel, "Cancel", pendingIntent)
val notif = NotificationCompat.Builder(this, channelId).apply {
setSmallIcon(R.drawable.logo)
setContentTitle(title)
setPriority(NotificationCompat.PRIORITY_LOW)
setColor(ContextCompat.getColor(this@MyService, R.color.colorPrimaryDark))
setColorized(true)
addAction(action)
}.build()
startForeground(1, notif)
return START_NOT_STICKY
}
在我的前台服务上单击action
按钮时,我要执行以下操作:
val db = FirebaseFirestore.getInstance()
db.collection("objs").document(id).delete()
而不是去我的MainActivity
。我希望调用在后台执行(单击该操作时不要打开我的应用程序)。
这可能吗?
答案 0 :(得分:1)
您可以使用BroadcastReceiver来执行操作
val intent = Intent(this, YourBroadCastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
this,
0,
intent,
0
)