【问题标题】:How to send firebase token to backend server from android studio如何从 android studio 将 firebase 令牌发送到后端服务器
【发布时间】:2020-08-03 02:50:55
【问题描述】:

我有一个 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());
    }
}

【问题讨论】:

  • 关注这个sample或者你可以得到这样的令牌sample
  • 您在使用 Firebase 云消息传递吗??
  • @Dharmaraj 是的,我正在使用 fcm
  • 那么你不需要编码来获取通知。只需添加 firebase 消息传递依赖项并从 Firebase 控制台发布通知
  • @Dharmaraj,我已经实现了,但我想从后端服务器发送通知,但我不知道如何使用用户数据和 fcm 令牌从 android studio 到后端服务器创建一个 post api

标签: android reactjs firebase progressive-web-apps


【解决方案1】:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
        public void onNewToken(String token) {
            super.onNewToken(token);
        //sendYourTokenToServer(token);
    }
}

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
  • 您好,欢迎来到 Stack Overflow!请拨打tour
猜你喜欢
  • 1970-01-01
  • 2021-08-01
  • 2016-04-21
  • 2018-06-16
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 2016-04-03
相关资源
最近更新 更多