android - 当应用程序在后台通知操作按钮不显示 android
问题描述
当应用程序处于后台时,我无法获取操作按钮。它仅在应用程序处于前台时才有效。
谁能帮我这个?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// create channel in new versions of android
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel("Test", "Test Notification", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
// show notification
Intent positive = new Intent(this, NotificationDismissedReceiver.class);
positive.putExtra("notiID", "1");
positive.setAction("Info");
Intent redoplacement = new Intent(this, NotificationDismissedReceiver.class);
redoplacement.putExtra("notiID", "2");
redoplacement.setAction("Call");
PendingIntent pIntent_positive = PendingIntent.getBroadcast(this, 1, positive, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent redoplacementintent = PendingIntent.getBroadcast(this, 2, redoplacement, PendingIntent.FLAG_CANCEL_CURRENT);
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 0 is request code
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "Test")
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent1)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.addAction(0, "Info", pIntent_positive)
.addAction(0, "Call", redoplacementintent);
// 0 is id of notification
notificationManager.notify(0, notificationBuilder.build());
}
解决方案
推荐阅读
- r - 有没有办法确定特定日期介于哪两个日期之间?
- vue.js - 如何同步不同的 .vue 文件
- ansible - Ansible 在标准输出输出中搜索子字符串
- redux - 如何调度一个动作,等待 1 秒,然后在 Redux Observable 中调度另外两个动作?
- wordpress - 将停放的域指向 Wordpress 子站点而不更改 url
- selenium-webdriver - 访问与 app_host 不同的 URL 时,Capybara 不会完全重置
- android - onClick 在带有 DataBinding 的 MVVM 中不起作用
- python - 使用 tika Python 3 更快地阅读 pdf 文件
- pointers - 检查指针是否指向 Nil Slice
- python - 我怎样才能使def函数不显示两次