无法解析方法“ builder()”

问题描述 投票:1回答:1

我想使用FCM发送主题通知,而无需使用Firebase控制台。当我在Firebase Documentation的帮助下构建新消息时,这是一个问题。

Picture of the problem

这里是我的代码:

 public void sendToTopic() {

        Message message = Message.builder()
                .putData("score", "850")
                .putData("time", "2:45")
                .setTopic("1")
                .build();


    }

public class MyFirebaseMessagingService extends FirebaseMessagingService {


    private static final String TAG = "MyFirebaseMsgService";
    public void onMessageReceived(RemoteMessage remoteMessage) {

    }


    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = ("asda");
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.book_icon)
                        .setContentTitle("Test")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

android firebase push-notification firebase-cloud-messaging
1个回答
1
投票

Message类位于Firebase admin sdk中,但您不能在android项目中使用它,只能在服务器端使用firebase admin sdk,在那里您将可以使用Message类。检查文档以供参考:-

Message

© www.soinside.com 2019 - 2024. All rights reserved.