gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:45:13 26 4
gpt4 key购买 nike

我编写了以下方法来发送通知,通知正在工作,但是当按下通知操作按钮设置时,不会打开 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com