1
POSTリクエストを直接https://fcm.googleapis.com/fcm/sendに送信することで、Androidアプリから直接プッシュ通知を送信する方法はありますか?もしそうなら、例を挙げてください。Firebase Android - App ServerなしでPOSTリクエストを使用して通知メッセージを送信するが、Androidアプリから直接送信する
POSTリクエストを直接https://fcm.googleapis.com/fcm/sendに送信することで、Androidアプリから直接プッシュ通知を送信する方法はありますか?もしそうなら、例を挙げてください。Firebase Android - App ServerなしでPOSTリクエストを使用して通知メッセージを送信するが、Androidアプリから直接送信する
JavaScriptを読むことができる場合は、simple-fcm-client example on react-native-fcmを確認してください。 Specifficaly、FirebaseClient.js
ファイル
私は完全を期すため、ここでのコードを投稿しますが、あなたはあなたのFirebaseコンソールから、あなたのAPIのWebキーと運命のデバイスの登録トークンとFirebaseConstants.KEYでtoken
を交換する必要があります。
const API_URL = "https://fcm.googleapis.com/fcm/send";
class FirebaseClient {
constructor() {
this.sendData = this.sendData.bind(this);
this.sendNotification = this.sendNotification.bind(this);
this.sendNotificationWithData = this.sendNotificationWithData.bind(this);
}
sendNotification(token) {
let body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO"
},
"priority": 10
}
this._send(JSON.stringify(body), "notification");
}
sendData(token) {
let body = {
"to": token,
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO",
"remote": true
},
"priority": "normal"
}
this._send(JSON.stringify(body), "data");
}
sendNotificationWithData(token) {
let body = {
"to": token,
"notification":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
"sound": "default",
"click_action": "fcm.ACTION.HELLO"
},
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (DATA)",
"click_action": "fcm.ACTION.HELLO",
"remote": true
},
"priority": "high"
}
this._send(JSON.stringify(body), "notification-data");
}
_send(body, type) {
let headers = new Headers({
"Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=" + FirebaseConstants.KEY
});
fetch(API_URL, { method: "POST", headers, body })
.then(response => console.log("Send " + type + " response", response))
.catch(error => console.log("Error sending " + type, error));
}
}
私のAndroidアプリ内でこのファイルを使用する必要がありますか? – Paul
このファイルはプロジェクトに入れるのではなく、アイデアを伝えるためのものです。 AndroidにはフェッチAPIもありますので、上記のコードを変更することは非常に簡単です。 –