0

我编写了以下方法来发送通知,通知正在工作,但是当按下通知操作按钮时settings,未打开通道设置的待处理意图。任何想法为什么这不起作用?

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

1 回答 1

0

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

我通过更改传递给操作按钮的待处理意图更新了上面的类(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);

然后我添加了一个接收待处理意图的服务类

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

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

它将请求转发到处理请求的 NotificationTasks 类,并根据意图的操作将其分配给适当的方法。

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);
    }
于 2020-03-29T08:53:33.627 回答