当应用程序处于后台时,我无法获取操作按钮。 仅当应用程序在前台时才起作用。
有人可以帮我吗?
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());
}