У меня проблема с отображением действий для уведомлений и намерений содержимого на Android.
Ниже кода работает нормально в эмуляторе , но на реальных кнопках действий устройства не отображаются, а ожидающие намерения имеют пустые дополнения .
Я тестировал приложение на Sony Xperia XA1 Ultra с Android 8.0 и Huawei Y6 с Android 6.0.
Я прочитал много веток, включая this , но я не нашел решения для этого случая.
Я использую библиотеки androidx.
Мой код:
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
int notificationId = Integer.parseInt(Objects.requireNonNull(message.getData().get("id")));
int zId = Integer.parseInt(Objects.requireNonNull(message.getData().get("zid")));
int status = Integer.parseInt(Objects.requireNonNull(message.getData().get("status")));
if (status == 2) {
NotificationReceiver.cancelNotification(this, notificationId);
} else {
Intent contentIntent = new Intent(this, SplashActivity.class);
contentIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
contentIntent.putExtra("param", true);
PendingIntent pContentIntent = PendingIntent.getActivity(this, UUID.randomUUID().hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent positiveIntent = new Intent(this, NotificationReceiver.class);
positiveIntent.putExtra("notificationId", notificationId);
positiveIntent.putExtra("zId", zId);
positiveIntent.setAction(POSITIVE_CLICK);
PendingIntent pPositiveIntent = PendingIntent.getBroadcast(this, notificationId, positiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent negativeIntent = new Intent(this, NotificationReceiver.class);
negativeIntent.putExtra("notificationId", notificationId);
negativeIntent.putExtra("zId", zId);
negativeIntent.setAction(NEGATIVE_CLICK);
PendingIntent pNegativeIntent = PendingIntent.getBroadcast(this, notificationId, negativeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
expandedView.setTextViewText(R.id.content_title, message.getNotification().getTitle());
expandedView.setTextViewText(R.id.content_text, message.getNotification().getBody());
expandedView.setOnClickPendingIntent(R.id.pButton, pPositiveIntent);
expandedView.setOnClickPendingIntent(R.id.nButton, pNegativeIntent);
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed);
collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
collapsedView.setTextViewText(R.id.content_title, message.getNotification().getTitle());
collapsedView.setTextViewText(R.id.content_text, message.getNotification().getBody());
collapsedView.setOnClickPendingIntent(R.id.pButton, pPositiveIntent);
collapsedView.setOnClickPendingIntent(R.id.nButton, pNegativeIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setContentTitle(Objects.requireNonNull(message.getNotification()).getTitle())
.setContentText(message.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(0)
.setVibrate(new long[0])
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
// setting the custom collapsed and expanded views
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
// setting style to DecoratedCustomViewStyle() is necessary for custom views to display
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(pContentIntent);
//notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.ic_done, getString(R.string.done), pPositiveIntent));
//notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.cancel), pNegativeIntent));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(notificationId, notificationBuilder.build());