java 消息传递示例_java – Firebase云消息传递API弹簧

以下是您可以实现此目的的方法:

第1步:

???在firebase上创建项目并生成服务器密钥.

第2步:

为fcm服务器生成json对象.这里的消息可能包含数据对象和通知对象.它还必须有接收器fcm id.样本json就像:

{

"notification":

{

"notificationType":"Test",

"title":"Title ",

"body":"Here is body"

},

"data":

{"notificationType":"Test",

"title":"Title ",

"body":"Here is body"

},

"to":"dlDQC5OPTbo:APA91bH8A6VuJ1Wl4TCOD1mKT0kcBr2bDZ-X8qdhpBfQNcXZWlFJuBMrQiKL3MGjdY6RbMNCw0NV1UmbU8eooe975vvRmqrvqJvliU54bsiT3pdvGIHypssf7r-4INt17db4KIqW0pbAkhSaIgl1eYjmzIOQxv2NwwwwXg"

}

第3步:

编写一个Rest调用者服务,它将通过以下url与fcm服务器通信:

https://fcm.googleapis.com/fcm/send

以下是示例工作代码:

public class PushNotificationServiceImpl {

private final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";

private final String FIREBASE_SERVER_KEY = "YOUR_SERVER_KEY";

public void sendPushNotification(List keys, String messageTitle, String message) {

JSONObject msg = new JSONObject();

msg.put("title", messageTitle);

msg.put("body", message);

msg.put("notificationType", "Test");

keys.forEach(key -> {

System.out.println("

Calling fcm Server >>>>>>>");

String response = callToFcmServer(msg, key);

System.out.println("Got response from fcm Server : " + response + "

");

});

}

private String callToFcmServer(JSONObject message, String receiverFcmKey) {

RestTemplate restTemplate = new RestTemplate();

HttpHeaders httpHeaders = new HttpHeaders();

httpHeaders.set("Authorization", "key=" + FIREBASE_SERVER_KEY);

httpHeaders.set("Content-Type", "application/json");

JSONObject json = new JSONObject();

json.put("data", message);

json.put("notification", message);

json.put("to", receiverFcmKey);

System.out.println("Sending :" + json.toString());

HttpEntity httpEntity = new HttpEntity<>(json.toString(), httpHeaders);

return restTemplate.postForObject(FIREBASE_API_URL, httpEntity, String.class);

}

}

你必须只调用sendPushNotification(List< String> receiverKeys,String messageTitle,String message)然后接收器将获得推送消息

谢谢 :)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Firebase应用内消息传递可以让你的Android应用实现实时通信和消息推送功能。下面是一些步骤来帮助你在Android应用上使用Firebase应用内消息传递: 1. 首先,在Firebase控制台中创建一个新的Firebase项目,并在项目设置中启用Firebase Cloud Messaging服务。 2. 在你的Android应用中,添加Firebase Core和Firebase Cloud Messaging依赖项。你可以通过Gradle文件来添加这些依赖项,例如: ``` implementation 'com.google.firebase:firebase-core:17.2.1' implementation 'com.google.firebase:firebase-messaging:20.0.0' ``` 3. 在你的AndroidManifest.xml文件中添加以下代码以注册Firebase Cloud Messaging服务: ``` <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> ``` 4. 创建一个MyFirebaseMessagingService类并继承FirebaseMessagingService。在这个类中,你可以处理接收到的消息并执行相应的操作,例如: ``` public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // 处理接收到的消息 String message = remoteMessage.getData().get("message"); // 执行相应的操作 } } ``` 5. 最后,在你的应用中发送消息,你可以使用Firebase Cloud Messaging API来发送消息,例如: ``` FirebaseMessaging.getInstance().send(new RemoteMessage.Builder("SENDER_ID" + "@gcm.googleapis.com") .setMessageId(Integer.toString(msgId.incrementAndGet())) .addData("message", "这是一条测试消息") .build()); ``` 以上是在Android应用上使用Firebase应用内消息传递的一些基本步骤,你可以根据你的需求进行相关的配置和定制化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值