Android 更新通知渠道设置的通知操作
Android notification action to update notification channel settings
我已经编写了以下方法来发送通知,通知正在运行,但是当按下通知操作按钮 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());
}
我设法找到了解决方案,不确定它是否是最快的方法,但至少它有效。
我通过更改传递给操作按钮的未决意图更新了上面的 class (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);
然后我添加了一个服务 class 来接收待处理的 intent
public class NotificationService extends IntentService
{
public NotificationService() {super("NotificationIntentService");}
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
NotificationTasks.executeTask(this, intent);
}
}
将请求转发给处理请求的 NotificationTasks class,并根据意图的操作将其分配给适当的方法。
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);
}
我已经编写了以下方法来发送通知,通知正在运行,但是当按下通知操作按钮 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());
}
我设法找到了解决方案,不确定它是否是最快的方法,但至少它有效。
我通过更改传递给操作按钮的未决意图更新了上面的 class (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);
然后我添加了一个服务 class 来接收待处理的 intent
public class NotificationService extends IntentService
{
public NotificationService() {super("NotificationIntentService");}
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
NotificationTasks.executeTask(this, intent);
}
}
将请求转发给处理请求的 NotificationTasks class,并根据意图的操作将其分配给适当的方法。
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);
}