7

I have seen a few apps that send push notifications that have custom buttons to take certain actions.

I have gone through the GCM push notification server document here: https://developer.android.com/google/gcm/server-ref.html#table1

but there is no reference on how to do this.

paishin
  • 258
  • 1
  • 5
  • 15

1 Answers1

3

This is done on the side of the device and not the server.

After finishing the complete implementation of Google Cloud Messaging

Consider creating Notifications on the device.

Here is snippet representing a notification with buttons, as pointed at the second link:

Notification notification = new Notification.Builder(context)
    // Show controls on lock screen even when user hides sensitive content.
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.ic_stat_player)
    // Add media control buttons that invoke intents in your media service
    .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
    .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1
    .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2
    // Apply the media style template
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1 /* #1: pause button */)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("Wonderful music")
    .setContentText("My Awesome Band")
    .setLargeIcon(albumArtBitmap)
    .build();
madlymad
  • 6,367
  • 6
  • 37
  • 68
  • Is there a way to show action buttons, when the application is closed? – Borzh Feb 13 '19 at 00:36
  • @Borzh the notification appears on the notification bar of the system indepently if the app is the foreground or not. – madlymad Feb 14 '19 at 20:24
  • Restart the phone / emulator, but don't start the app. Send the notification. You will see that nothing happens. This is because once you start the app, your FCM service remains active until next restart. But when you restart, without opening the app, the service is not active. There are no magic in this world. – Borzh Feb 15 '19 at 02:39
  • 1
    @Borzh you are missing some other code... I would suggest to create a new question with all the details and what you tried. This is the purpose of push FCM messages... From the user perspective every time I reboot my device facebook and other apps post me notifications so there is a way to do it. Maybe you should use the boot/restart broadcast https://stackoverflow.com/questions/53847965/android-boot-completed-notification-not-received-after-reboot/53848560#53848560 – madlymad Feb 15 '19 at 21:53