我正在使用Java为IOS发送firebase推送通知。下面是我的代码。
public class SendNotifi {
public final static String AUTH_KEY_FCM = "AIzaSyBgF...............";
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 - IOS"); // Notification title
info.put("body", "Hello Test notification - IOS"); // Notification body
info.put("badge", "1");
json.put("notification", info);
json.put("priority", "high");
System.out.println("json : " +json);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
public static void main(String main[])
{
try {
SendNotifi.pushFCMNotification("e_0k8MPAXpI:APA91bFrz3MkWS0V9E_PJMGwtFppYhR6ap9rD53nB-Wxkosij1jDDuPDXRw__l4tzOOsGaEm_j02a20oJGLimKvTuZSqRs6aTcbizTMuYMp6_1jB4U7RCl2A_NdWEHIMlaAl6YN1o_Hv");
} catch (Exception e) {
e.printStackTrace();
}
}
}
IOS无法正常工作。
我在回复中获得了确认成功,但我没有在IOS中获得任何推动力。
{"multicast_id":6591278961512996707,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1474009869605736%2f4186c42f4186c4"}]}
提前致谢。
答案 0 :(得分:0)
首先,我要提到 FCM 有两种类型的消息有效负载。通知和数据。请参阅此处的文档
通过 Firebase 通知控制台发送通知时,它将被视为通知负载。但是,如果您添加自定义数据,它会将其作为自定义键值对添加到负载中。
例如,在您的帖子中,FCM 负载应如下所示:
{
"notification": {
"body": "Some one invited you.",
"title": "Friend request notification",
"mutable-content": true,
"icon": "myicon",
"sound": "tri-tone"
},
"data": {
"category": "provider-body-panel",
"mutable-content": true,
"click_action": "provider-body-panel"
}
}
"registration_ids": ["<REGISTRATION_TOKEN_HERE>"],
"priority": "high"
}
怎么了? mutable-content 应该是 mutable_content(注意下划线)并且应该与通知处于同一级别。 (这个我可能误解了,但是)FCM 没有类别参数,click_action 已经对应它。 请参阅此处的文档了解参数。
目前无法在使用 Firebase 通知控制台时设置 click_action 和 mutable_content 的值。您必须自己构建有效负载,如下所示:
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Some one invited you.",
"title": "Friend request notification",
"click_action" : "provider-body-panel"
}
}
然后从您自己的应用服务器发送它。您也可以使用 Postman 或 cURL 来执行此操作。
这对我有用,希望你也是。