2

新しいFirebase通知サービスを使用すると、コンソールUIを使用してすべてのモバイルアプリユーザーに通知を送信できます。サーバー側コードによるFirebase通知

Firebase Notifications Service用のREST APIが見つかりませんでした。私は、イベントに基づいてサーバー側のコードを使用して、モバイルユーザーに通知を自動的に送信したいと考えています。 Firebase Notifications ServiceにはHTTP/S経由でアクセスできるAPIがありますか?

+0

あなたは文書を読んでいますか? https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server – tyczj

答えて

2

ほとんど! https://firebase.google.com/docs/cloud-messaging/downstream

主な違いは、通知コンソールから送信されたメッセージに自動Firebase Analyticsのトラッキングがあることです。送信したメッセージの場合、手動で追跡するイベントを追加することができます。

+0

ありがとうございます。これを試してみる。 –

10

はい、できます。

<?php 
$ch = curl_init("https://fcm.googleapis.com/fcm/send"); 

//The device token. 
$token = "device_token_here"; 

//Title of the Notification. 
$title = "The North Remembers"; 

//Body of the Notification. 
$body = "Bear island knows no king but the king in the north, whose name is stark."; 

//Creating the notification array. 
$notification = array('title' =>$title , 'body' => $body); 

//This array contains, the token and the notification. The 'to' attribute stores the token. 
$arrayToSend = array('to' => $token, 'notification' => $notification); 

//Generating JSON encoded string form the above array. 
$json = json_encode($arrayToSend); 

//Setup headers: 
$headers = array(); 
$headers[] = 'Content-Type: application/json'; 
$headers[] = 'Authorization: key= your_server_key_here'; 

//Setup curl, add headers and post parameters. 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);  

//Send the request 
curl_exec($ch); 

//Close request 
curl_close($ch); 

?> 

Project Settings -> Cloud Messaging Tab -> Copy the Server key. 

2)、ここでは特定のデバイスに通知を送信するためのサンプルのPHPスクリプトは次のとおりです。

1)まず、あなたのfirebaseプロジェクトのサーバーキーを取得実行上の

3)出力:

{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]} 
+1

";"を追加する必要があります。 $ titleの終わりに$ arryToSendの名前を$ arrayToSendに変更しますが、コードはうまく機能します。ありがとう! – ZLNK

2

@ Narendra Naidu、こんにちは、サーバー側のプッシュ通知用にこのコードスニペットを試すことができます。あなたのサーバー側のプロジェクトコードに単純なJavaクラスを作成し、このメソッドにパラメータを追加すると、これを行うためのfirebase資格が必要になります。以下を試してください。

// Method to send Notifications from server to client end. 
public final static String AUTH_KEY_FCM = "ApidhfkIjd_cAdhpa-ZZ065hskiH53Hw3g"; 
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send"; 
// userDeviceIdKey is the device id you will query from your database  
public static void pushFCMNotification(String userDeviceIdKey) throws  Exception{ 

String authKey = AUTH_KEY_FCM; // You FCM AUTH key 
String FMCurl = API_URL_FCM;  

URL url = new URL(FMCurl); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

conn.setUseCaches(false); 
conn.setDoInput(true); 
conn.setDoOutput(true); 

conn.setRequestMethod("POST"); 
conn.setRequestProperty("Authorization","key="+authKey); 
conn.setRequestProperty("Content-Type","application/json"); 

JSONObject json = new JSONObject(); 
json.put("to",userDeviceIdKey.trim()); 
JSONObject info = new JSONObject(); 
info.put("title", "Notificatoin Title"); // Notification title 
info.put("body", "Hello Test notification"); // Notification body 
json.put("notification", info); 

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
wr.write(json.toString()); 
wr.flush(); 
conn.getInputStream(); 
} 

このrefのドキュメントを通過してください:

  1. https://firebase.google.com/docs/cloud-messaging/http-server-ref
  2. https://firebase.google.com/docs/cloud-messaging/server

それはあなたのサーバーからに通知を送信するためにあなたのサーバー側の情報を提供 - firebaseサーバ - クライアントアプリケーションに送信します。 また、これについてはいくつかの簡単なアイデアを得るプレーンなJavaコードファイル(サーバーエンドクラス)を以下で見つけてください。

さらに詳しい情報があれば教えてください。

+1

こんにちはSandeep_Devhare、あなたはFcmを使用して複数のデバイスの通知を送信するJavaクラスがありますか? – Priya

+0

@Priya、実際は*** FCM ***には、複数のデバイスにメッセージを送信する2つの方法があります。***特定のトピックにオプトインした複数のデバイスにメッセージを送信できるトピックメッセージ***。 ***定義したグループに属する複数のデバイスにメッセージを送信できるデバイスグループメッセージング***。このhttps://firebase.google.com/docs/cloud-messaging/android/send-multipleの公式ドキュメントを参照してください。 –

+0

こんにちは、このコードは、SSLセキュリティを持つサーバーでは動作しません。それは私に例外 "No Subjectの代替DNS名fcm.googleapis.com"を与えます。この例外に関する考え方は? –