首页 > 解决方案 > 通知气泡未显示

问题描述

出于某种原因,每当我发送通知时,通知气泡都不会出现。我允许来自开发人员选项的通知气泡,compileSdkVersion 是 29。气泡活动中只有一个按钮用于测试目的。任何帮助将不胜感激。

我的代码:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);

            // set up notification channel
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription(CHANNEL_DESCRIPTION);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }

            // create person
            Person person = new Person.Builder()
                    .setName("Notification")
                    .setIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
                    .setBot(true)
                    .setImportant(true)
                    .build();

            // create bubble meta data
            Intent target = new Intent(mContext, BubbleActivity.class);
            PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, PendingIntent.FLAG_UPDATE_CURRENT);

            Notification.BubbleMetadata bubbleMetadata = new Notification.BubbleMetadata.Builder()
                    .setDesiredHeight(300)
                    .setIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
                    .setSuppressNotification(true)
                    .setAutoExpandBubble(false)
                    .setIntent(bubbleIntent)
                    .build();

            // create message style
            Notification.MessagingStyle style = new Notification.MessagingStyle(person)
                    .setGroupConversation(false)
                    .addMessage("notification", currentTimeMillis(), person);

            // create notification intent
            Intent target2 = new Intent(mContext.getApplicationContext(), LaunchActivity.class);
            PendingIntent notificationIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 1, target2, PendingIntent.FLAG_UPDATE_CURRENT);

            // create notification
            Notification notification = new Notification.Builder(mContext, CHANNEL_ID)
                    .setContentTitle("Notification")
                    .setSmallIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
                    .setLargeIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setStyle(style)
                    .setBubbleMetadata(bubbleMetadata)
                    .addPerson(person)
                    .setShowWhen(true)
                    .setContentIntent(notificationIntent)
                    .build();

            if (notificationManager != null) {
                notificationManager.notify(0, notification);
            }

        }

标签: androidnotifications

解决方案


您需要在通知上设置快捷方式 ID。完成后,您的通知将出现在对话空间中,并允许冒泡。请注意,气泡最初不会自动冒泡。由用户调用/触发它。

请参考以下资源: https ://developer.android.com/guide/topics/ui/conversations#api-shortcuts

https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setShortcutId(java.lang.String)


推荐阅读