如何从Android Studio将Firebase令牌发送到后端服务器

时间:2020-04-20 14:43:50

标签: android reactjs firebase progressive-web-apps

我有一个React Web应用程序,并将其用作移动应用程序并用于推送通知,我正在使用Firebase,并通过Firebase控制台实现了它,我想从后端服务器发送通知,但是我是android studio中的新手,所以我无法找出如何在私有api调用中将firebase令牌发送到后端服务器。我的用户数据在redux存储中,我必须在FirebaseMessagingService文件中发出私有api发布请求。 任何帮助将不胜感激。

下面是我的FirebaseMessagingService文件中的代码

package com.example.goldfish;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class MyMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull 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, "MyNotifications")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true)
                .setContentText(message);
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());
    }
}

1 个答案:

答案 0 :(得分:0)

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
        public void onNewToken(String token) {
            super.onNewToken(token);
        //sendYourTokenToServer(token);
    }
}