joke4me
joke4me

Reputation: 842

Anndroid : How to send broadcast intent to service from notification action?

I'm trying to send a broadcast intent from within a notification action on Android but kind of lost here.

I have an android service that recieves broadcast intents from activities/frgaments and react accordingly, which works fine but I don't know hoe to send one from a notification action.

Service looks something like this.

class MyService : Service() {

    override fun onCreate() {
        super.onCreate()

        LocalBroadcastManager.getInstance(this).registerReceiver(mIntentActionBroadcastReceiver, IntentFilter(SERVICE_BROADCAST_INTENT))

        val broadcastIntentFilter = IntentFilter()
        broadcastIntentFilter.addAction(SERVICE_BROADCAST_KEY)
        registerReceiver(mIntentActionBroadcastReceiver, broadcastIntentFilter)
    }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        // onStart
    }

    private val mIntentActionBroadcastReceiver = object : BroadcastReceiver() {
         override fun onReceive(context: Context, intent: Intent) {
            // do something
        }
    }
}

Now I have notification channel where the user performs certain actions, it looks something like this.

val notificationBuilder = NotificationCompat.Builder(context, PRIMARY_CHANNEL)
    .addAction(getCustomAction(context))

    private fun getCustomAction(context: Context): NotificationCompat.Action {
        val actionIntent = Intent(context, MyService::class.java).apply {
            action = NOTIFICATION_ACTION_CUSTOM
        }
        val pendingIntent = PendingIntent.getService(context, 0, actionIntent, 0)
        return NotificationCompat.Action(R.drawable.ic_custom_48dp, "", pendingIntent)
    }

When the user click the action in the notification, it seems to re-call MyService onStartCommand again, thus certain state of my service is messed up.

Instead, is it possible to send a broadcast intent from getCustomAction() and recieve it in the service and do something with it?

Would something like this be possible within getCustomAction()?

val intent = Intent(SERVICE_BROADCAST_INTENT)
intent.putExtra(SERVICE_BROADCAST_KEY,  NOTIFICATION_ACTION_CUSTOM)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

Upvotes: 0

Views: 1554

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006859

is it possible to send a broadcast intent from getCustomAction() and recieve it in the service and do something with it?

You could have the service register a system broadcast receiver (registerReceiver() on Context), then send a system broadcast to it (sendBroadcast() on Context) using PendingIntent.getBroadcast() to create the PendingIntent.

Or, fix the service to address "thus certain state of my service is messed up".

Would something like this be possible within getCustomAction()?

No, because the sender is another process, not yours. LocalBroadcastManager only works within your own process.

Upvotes: 2

Related Questions