首页 > 解决方案 > 如何在屏幕上显示通知。(火力基地)

问题描述

我的应用程序正在使用 firebase 推送通知,通知工作正常。我想做的是像 snapchat 和许多应用程序那样在屏幕上显示通知,但我不知道该怎么做:/

在此处输入图像描述

 public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage); 
                showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

     public void showNotification(String title, String message) {

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotifitcation")
                    .setContentTitle(title)
                    .setSmallIcon(R.drawable.logo)
                    .setAutoCancel(true)
                    .setContentText(message);

            NotificationManagerCompat manager = NotificationManagerCompat.from(this);
            manager.notify(999, builder.build());
        }

标签: androidfirebasepush-notificationnotificationsfirebase-cloud-messaging

解决方案


如果您想在应用程序的另一部分显示通知,您可以将通知存储在本地数据库(领域、房间、sqlite)中,然后制作一个显示来自数据库的通知的屏幕。

如果你想做一个快速的概念证明,你可以存储在内存中,在一个单例中(仅用于快速的概念证明,请不要到处使用单例)。


推荐阅读