【问题标题】:Android - push notifications: purpose of FirebaseMessagingService class?Android - 推送通知:FirebaseMessagingService 类的用途?
【发布时间】:2019-01-15 01:27:27
【问题描述】:

我看过两个关于通过 Firebase 云消息从服务器使用 PHP 向安卓设备发送推送消息的教程,它工作正常。但是两个教程都有 FirebaseMessagingService 类,我不明白它的目的是什么?他们没有对此做任何解释。

就是这样:

class FirebaseMessagingService : com.google.firebase.messaging.FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        showNotification(remoteMessage!!.data["message"]!!)
    }

    private fun showNotification(message: String) {

        val i = Intent(this, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

        val pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)

        val builder = NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("FCM Test")
                .setContentText(message)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentIntent(pendingIntent)

        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        manager.notify(0, builder.build())
    }
}

我可以用它从 android 发送推送通知吗?我只需要从 PHP 发送推送消息,我可以删除它吗?我的项目概述已经很混乱,很难理解,我不想在里面放任何无用的东西。提前致谢

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    这个类负责处理收入消息。无论您的应用程序在前台还是后台,当有传入的data 消息时,都会调用函数onMessageReceived。更具体地说,此方法是为您自定义 key:value 对在 data 有效负载中的处理

    如果你不打算做任何特殊处理,你可以把它留在那里。 请阅读here了解更多信息。

    【讨论】:

    • 谢谢你的回答,但我还是不完全明白,好的,所以当我删除这个类时,我不会再收到任何从 PHP 发送的消息了? setContentTitle setContentText 有哪些选项?在哪些情况下我需要自定义它们??
    • 是的,如果您删除了此类,您将不会收到来自 firebase 的任何消息。对于您询问的两个功能,它们是您可以在通知托盘中看到的通知的标题和正文。有许多用例需要开发人员自定义onMessageReceived 函数。例如,您需要在用户点击通知时将其带到特定页面。在这种情况下,您需要将一些数据放入 Intent 并在起始页面中检索它,以便您知道应该去哪里。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多