java - 如何设置通知的标题和内容?

标签 java android android-notifications

我正在尝试在收到新消息时向用户发送通知。

我可以发送通知,但我在自定义通知时遇到了一些问题。

这是我创建 notificationChannel 的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NotificationName",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("Test channel");

            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 2000});
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);

            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

这是创建通知的代码:

notificationBuilder.setAutoCancel(true)
                .setVibrate((new long[]{0, 1000, 500, 2000}))
                .setStyle((new NotificationCompat.InboxStyle() ) )
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_notification)
                .setContentTitle("Unread message")
                .setContentText("You have an unread message")
                .setOnlyAlertOnce(true)
                .setContentInfo("Info");

        Intent intent = new Intent(this, bildirimDeneme.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(contentIntent);

        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());

我将内容文本设置为“您有一条未读消息”。但通知显示如下 [1]:https://hizliresim.com/EO6rVB

所以根本就没有内容。

最佳答案

我想您必须像这样将内容设置为 InboxStyle:

notificationBuilder.setStyle(new Notification.InboxStyle()
     .setContentTitle("Title")
     .addLine("Hello")
     .addLine("World")
     .setSummaryText("+3 more"))

关于java - 如何设置通知的标题和内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56883210/

相关文章:

java - 带有预装插件的 Eclipse

java - 如何使用 Scanner 以一个整数获取存储输入

java - 如何从 3 个整数组成 HashMap 的键?

android - 没有 ActionBar 的基于 fragment 的选项卡

android - 构建 ios 应用程序 Cordova 时找不到“Cordova/CDV.h”文件

Android 通知声音不播放,间歇性

java - 像 jsfiddle 这样的在线 Java 暂存器

android - 抖动图片可以具有相同的文件名吗?

android - 在 GCM 通知上打开 APP

android - FLAG_ACTIVITY_CLEAR_TOP 在通知部分不起作用