Authenticate a service account and authorize it to access Firebase services
you must generate a private key file in JSON format.
To generate a private key file for your service account:
In the Firebase console, open Settings > Service Accounts.
Click Generate New Private Key, then confirm by clicking Generate Key.
Securely store the JSON file containing the key.
使用以上json文件初始化FCM Admin SDK
FileInputStream serviceAccount =
new FileInputStream("path/to/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
如果是通过设置环境变量 GOOGLE_APPLICATION_CREDENTIALS
指定json文件的位置, 则以上代码修改为:
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
有三个topic要谈:
消息格式: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
pom.xml
最新版本请参见: https://firebase.google.com/docs/admin/setup#add-sdk
Authenticate a service account and authorize it to access Firebase services
you must generate a private key file in JSON format.
To generate a private key file for your service account:
使用以上json文件初始化FCM Admin SDK
如果是通过设置环境变量
GOOGLE_APPLICATION_CREDENTIALS
指定json文件的位置, 则以上代码修改为:
例子:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
或者使用以上json文件得到一个short-lived JWT token
如果要使用FCM service, SCOPES的值必须为
https://www.googleapis.com/auth/firebase.messaging
使用JWT Token
以上整理自: https://firebase.google.com/docs/cloud-messaging/auth-server
搭建FCM Server的要求
https://firebase.google.com/docs/cloud-messaging/server#role
搭建FCM Server的两种方式
发送消息 (cyper实战)
App端 用到的 Dart package (pub spec)
Pub Spec: firebase_messaging 6.0.13
References
Spring Boot: Send push notifications from Spring Boot server-side application using FCM
新鲜出炉的:Flutter Push Notifications using flutter firebase messaging with example
Concise: FCM Push Notifications for Flutter
FilledStacks: Push Notifications in Flutter using Firebase