javaandroidandroid-pendingintentnotification-channel

Android notification action to update notification channel settings


I've written the following method to send notificaitons, notifications are working however when the notification action button settings is pressed, the pending intent to the channel settings isn't opened. Any ideas why this isn't working?

private static final String NOTIFICATION_GROUP_ID = "notification_group";
private static final int NOTIFICATION_ID = 546893;
private static final String NOTIFICATION_CHANNEL_ID = "notification_channel";

public static void sendNotification(Context context, int iconId, String title, String message, String channelId, int notificationId)
{
    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
    NotificationChannel notificationChannel = new NotificationChannel(
            channelId, title, NotificationManager.IMPORTANCE_HIGH);

    notificationManager.createNotificationChannel(notificationChannel);

    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, NOTIFICATION_ID);
    PendingIntent settingsIntent = PendingIntent.getActivity(context, 0, intent, 0);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
            .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
            .setSmallIcon(iconId)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setGroup(NOTIFICATION_GROUP_ID)
            .addAction(iconId, "settings", settingsIntent)
            .setAutoCancel(true);

    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}

Solution

  • I managed to find a solution, not sure if it's the quickest way but it works at least.

    I updated the class above (NotificationUtils) by altering the pending intent that is passed to the action button:

    Intent intent = new Intent(context, NotificationService.class);
    intent.setAction(NotificationTasks.ACTION_UPDATE_NOTIFICATION_SETTINGS);
    intent.putExtra("channel-id", channelId);
    PendingIntent settingsIntent = PendingIntent.getService(context, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    I then added a service class that receives the pending intent

    public class NotificationService extends IntentService
    {    
        public NotificationService() {super("NotificationIntentService");}
    
        @Override
        protected void onHandleIntent(@Nullable Intent intent)
        {
            NotificationTasks.executeTask(this, intent);
        }
    }
    

    Which forwards the request to the NotificationTasks class that handles the request and assigns it to the appropriate method based on the action of the intent.

    public static void executeTask(Context context, Intent intent)
        {
            switch(intent.getAction())
            {
                case ACTION_UPDATE_NOTIFICATION_SETTINGS:
                    updateNotificationSettings(context, intent);
                    break;
            }
        }
    
    private static void updateNotificationSettings(Context context, Intent intent)
        {
            Intent updateNotificationSettingIntent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
            updateNotificationSettingIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            updateNotificationSettingIntent.putExtra(Settings.EXTRA_CHANNEL_ID, intent.getStringExtra("channel-id"));
            context.startActivity(updateNotificationSettingIntent);
        }