【问题标题】:How to display notification on the screen. (firebase)如何在屏幕上显示通知。 (火力基地)
【发布时间】:2019-07-10 17:54:43
【问题描述】:

我的应用正在使用 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());
        }

【问题讨论】:

  • 此方法在您的应用处于前台时触发。所以尝试发送数据通知。这将触发此方法。

标签: android firebase push-notification notifications firebase-cloud-messaging


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2016-09-17
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多