Zorgan
Zorgan

Reputation: 9123

Perform function in PendingIntent

I want to execute an async call to my Firebase Firestore in my PendingIntent:

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
}

When the action button is clicked on my foreground service, I want to perform this:

val db = FirebaseFirestore.getInstance()
db.collection("objs").document(id).delete()

instead of going to my MainActivity. I want the call to execute in the background (not opening my app when the action is clicked).

Is this possible?

Upvotes: 1

Views: 307

Answers (1)

Ankit
Ankit

Reputation: 1068

You can use BroadcastReceiver to perform your action

    val intent = Intent(this, YourBroadCastReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
        this,
        0,
        intent, 
        0
    )

Upvotes: 1

Related Questions