Android推送通知>从Legacy FCM API迁移到HTTP V1 API>有效载荷
创始人
2024-08-21 20:30:22
0

以下是一个使用HTTP V1 API发送推送通知并包含有效载荷的示例代码:

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.AndroidConfig;
import com.google.firebase.messaging.AndroidNotification;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;

import java.io.FileInputStream;
import java.io.IOException;

public class FirebaseCloudMessaging {

  public static void main(String[] args) throws IOException {
    FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(serviceAccount))
        .build();

    FirebaseApp.initializeApp(options);

    // 创建消息
    Message message = Message.builder()
        .setToken("device_token")
        .setNotification(new Notification("标题", "正文"))
        .putData("key1", "value1")
        .putData("key2", "value2")
        .setAndroidConfig(AndroidConfig.builder()
            .setPriority(AndroidConfig.Priority.HIGH)
            .build())
        .build();

    // 发送消息
    String response = FirebaseMessaging.getInstance().send(message);
    System.out.println("成功发送消息:" + response);
  }
}

请注意,上面的代码需要使用Firebase Admin SDK的服务帐号密钥(serviceAccountKey.json)。您需要将路径“path/to/serviceAccountKey.json”替换为您自己的密钥文件的路径。

此示例代码使用Firebase Admin SDK的Java库。您需要在项目中包含以下Maven依赖项:


  com.google.firebase
  firebase-admin
  7.3.0

确保将版本号与您正在使用的Firebase Admin SDK版本匹配。

这个示例代码会向指定设备令牌发送推送通知,并在有效载荷中包含键值对数据。您可以根据您的需求自定义通知标题和正文,以及有效载荷数据。

请确保已正确设置Firebase项目和配置Firebase Cloud Messaging以使用HTTP V1 API。

相关内容

热门资讯

举措透视!wepoker游戏安... 举措透视!wepoker游戏安装教程,wepoker有辅助插件吗,第三方教程(有挂秘笈)-哔哩哔哩>...
发现透视"wepok... 发现透视"wepoker辅助工具好用吗"开挂(透视)辅助器(9分钟了解高科技教程)发现透视"wepo...
模块透视!hhpoker可以控... 【亲,hhpoker可以控制吗 这款游戏可以开挂的,确实是有挂的,很多玩家在这款hhpoker可以控...
实测透视"微扑克发牌... 实测透视"微扑克发牌机制详解"开挂(透视)辅助器(玩家亲测黑科技教程)实测透视"微扑克发牌机制详解"...
秘籍透视!wepoker可以透... >>您好:wepoker辅助器官方确实是有挂的,很多玩家在这款wepoker辅助器官方游戏中打牌都会...
正版透视"wepok... 正版透视"wepoker透视软件下载"开挂(透视)辅助软件(科技分享揭秘教程)正版透视"wepoke...
模块透视!pokemmo辅助官... wepoker模拟器哪个好用 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四...
曝光透视"微扑克辅助... 曝光透视"微扑克辅助期是干嘛的"开挂(透视)辅助工具(记者发布总结教程)曝光透视"微扑克辅助期是干嘛...
大纲透视!约局吧辅助脚本,aa... aapoker安装包怎么使用是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
必看透视"微扑克辅助... 必看透视"微扑克辅助器官网入口"开挂(透视)辅助脚本(今日公布必赢教程)必看透视"微扑克辅助器官网入...