My app plays music and when users open notifications screen by swiping from the top of the screen ( or generally from the bottom right of the screen on tablets ), I want to present them a button to stop the currently playing music and start it again if they want.
I am not planning to put a widget on the user's home screen, but just into notifications. How can I do this?
You can create an intent for the action (in this case stop playing) and then add it as an action button to your notification. Intent snoozeIntent = new Intent(this, MyBroadcastReceiver. class); snoozeIntent. setAction(ACTION_SNOOZE); snoozeIntent.
What Are Push Notification Action Buttons? This is a case where the name is actually pretty self-explanatory: Push notification action buttons are literal buttons that can be added to the push notifications you send in order to allow recipients to take action on the messages they receive.
You may want to consider using awesome_notifications plugin in the meantime since it has support for notification buttons. You can then add the icon for the Action Button by adding the icon property on NotificationActionButton. The icon should be a String resource of the image mapped in the assets.
You can create an intent for the action (in this case stop playing) and then add it as an action button to your notification.
Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class); snoozeIntent.setAction(ACTION_SNOOZE); snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0); PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .addAction(R.drawable.ic_snooze, getString(R.string.snooze), snoozePendingIntent);
Please refer to the Android documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With