【发布时间】: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());
}
}
【问题讨论】:
-
您在使用 Firebase 云消息传递吗??
-
@Dharmaraj 是的,我正在使用 fcm
-
那么你不需要编码来获取通知。只需添加 firebase 消息传递依赖项并从 Firebase 控制台发布通知
-
@Dharmaraj,我已经实现了,但我想从后端服务器发送通知,但我不知道如何使用用户数据和 fcm 令牌从 android studio 到后端服务器创建一个 post api
标签: android reactjs firebase progressive-web-apps