java - 如何更改 Android 应用程序中的通知设置?
问题描述
如何以编程方式打开所有这些设置?
我注意到当您安装 WhatsApp 时,它们一开始都是打开的(请看下图)。
但我找不到以编程方式打开它们的方法。
这是我发送通知的方式:
private void sendNotification(Intent intent){
Context context = NotificationService.this;
//open the activity after the notification is clicked
Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0);
Notification.Builder builder = new Notification.Builder(context)
.setTicker("Notification")
.setContentTitle("Important Message")
.setContentText("This is an example of a push notification using a Navigation Manager")
.setSmallIcon(R.drawable.ic_add)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//These are necessary for the notification to pop up
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(Notification.PRIORITY_MAX);
builder.setSound(alarmSound);
builder.setLights(Color.BLUE, 500, 500);
}
//after android O we must use notification channels
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Reminder to remind to review your notes",
NotificationManager.IMPORTANCE_HIGH);
if(alarmSound != null){
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
channel.setSound(alarmSound,att);
}
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
channel.setDescription("Hello Dear friends"); //this is to test what this is
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setVibrationPattern(new long[]{300, 300, 300});
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notification = builder.build();
notificationManager.notify(0, notification);
}
我还将此权限添加到清单中:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
更新: 在模拟器上使用此代码,我会收到提示通知。但在我的小米设备上,没有提示通知。它只出现在状态栏上。如果我手动打开浮动通知(您可以在照片中看到),那么我会收到提醒通知。但默认情况下它们是关闭的。当您安装 Whatsapp 时,它们都已打开。这是 Whatsapp 的一种特权,因为它很出名吗?还是有办法做到这一点?
解决方案
我已经尝试过,但无法以编程方式设置这些设置。而不是这个,我使用以下方法打开通知设置屏幕以启用通知声音/振动。
private void openNotificationSettingsForApp(String channelId) {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && channelId!=null){
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,channelId);
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
}
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
}
推荐阅读
- javascript - React Native - 执行 UI 块时抛出异常 - 无法识别的选择器?
- python-3.x - 在 Django 中从视图到 url 获取字符串变量
- reactjs - React - 状态是在一个父函数中定义的,但不是在从子函数调用的另一个父函数中
- python - 带有 3D 输入的 Keras 中的 preprocess_input() 方法
- mysql - 计算Mysql中的出现次数
- mysql - Express 永远加载 mySQL 查询?
- java - 为什么我的 lambda 表达式有效,但 actionPerformed() 方法无效?
- javascript - Livewire 和 FilePond。更改图像顺序
- java - 如何在 Java ArrayList 中找到多种模式?
- webpack - 无法从 v4 webpack 到 v5