android - 使用 Firebase 云消息传递时消息无法转换为 RemoteMessage 错误

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

<分区>

有人问过类似的问题here但没有得到答复。 我正在尝试使用 FCM 并使用官方 Firebase 示例中提到的代码向设备发送设备通知 from github :

public void sendToToken() throws FirebaseMessagingException {
        // [START send_to_token]
        // This registration token comes from the client FCM SDKs.
        String registrationToken = YOUR_REGISTRATION_TOKEN;

        // See documentation on defining a message payload.
        Message message = Message.builder()
                .putData("score", "850")
                .putData("time", "2:45")
                .setToken(registrationToken)
                .build();

        // Send a message to the device corresponding to the provided
        // registration token.
        String response = FirebaseMessaging.getInstance().send(message);
        // Response is a message ID string.
        System.out.println("Successfully sent message: " + response);
        // [END send_to_token]
    }

但是在运行应用程序时,我收到此错误:

error: incompatible types: Message cannot be converted to RemoteMessage

最佳答案

您对 Message 对象的导入有误。请导入以下消息

import com.google.firebase.messaging.RemoteMessage;

使用

RemoteMessage message = new RemoteMessage(bundle);

关于android - 使用 Firebase 云消息传递时消息无法转换为 RemoteMessage 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072661/

相关文章:

android - 设置 recyclerview 中项目的悬停

android - 他们是一种手动重新生成推送通知 token 的方法(iOS、Android、Windows)

android - android.permission.PACKAGE_USAGE_STATS 是否仅适用于系统应用程序?

当布局变为横向时,Android 应用程序终止

javascript - AngularFire2 AuthCredentials

javascript - 获取图像 url Firebase 存储(管理员)

javascript - 设置没有删除项目的状态

ios - 如何使用 Firebase 读取推送通知中发送的自定义数据

ios - Linux APNS 服务器我应该创建哪个证书?

Android DataBinding, View 标签在 View :null 上不正确