java - 用于更新通知 channel 设置的 Android 通知操作

标签 java android android-pendingintent notification-channel

我编写了以下方法来发送通知,通知正在工作,但是当按下通知操作按钮设置时,不会打开 channel 设置的挂起 Intent 。有什么想法为什么这不起作用吗?

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());
}

最佳答案

我设法找到了一个解决方案,不确定这是否是最快的方法,但至少有效。

我通过更改传递给操作按钮的挂起 Intent 来更新上面的类 (NotificationUtils):

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);

然后我添加了一个接收挂起 Intent 的服务类

public class NotificationService extends IntentService
{    
    public NotificationService() {super("NotificationIntentService");}

    @Override
    protected void onHandleIntent(@Nullable Intent intent)
    {
        NotificationTasks.executeTask(this, intent);
    }
}

它将请求转发到NotificationTasks类,该类处理请求并根据 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);
    }

关于java - 用于更新通知 channel 设置的 Android 通知操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60875491/

相关文章:

java - 在需要该类实例的类中使用静态方法

java - 从我的DialogFragment中的MainActivity访问ArrayList <String>遇到麻烦

java - 执行 com.android.build.gradle.internal.tasks.FinalizeBundleTask$BundleToolRunnable 时发生故障

android - 为什么即使设置了 FLAG_NO_CREATE,PendingIntent 也会触发 onCreate

android - New PendingIntent 更新当前 Intent

java - 基于键值的值范围

Java 扫描器和正则表达式 : Applying findWithinHorizon twice returns no results

java - 检查字符串是否包含 CJK(中文)字符

android - 蓝牙两次而不是一次添加到 ListView

Android:通过 ContentIntent 传输数据