无法使用java在IOS中发送firebase pushnotification
我正在使用Java for IOS发送firebase推送通知。下面是我的代码无法使用java在IOS中发送firebase pushnotification,ios,push-notification,firebase-cloud-messaging,Ios,Push Notification,Firebase Cloud Messaging,我正在使用Java for 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
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"}]}
提前感谢。首先,我要提到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"
}
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Some one invited you.",
"title": "Friend request notification",
"click_action" : "provider-body-panel"
}
}
{
“通知”:{
“身体”:“有人邀请你。”,
“标题”:“好友请求通知”,
“可变内容”:正确,
“图标”:“我的图标”,
“声音”:“三声”
},
“数据”:{
“类别”:“提供者主体面板”,
“可变内容”:正确,
单击“操作”:“提供程序正文面板”
}
}
“注册ID”:[“”],
“优先级”:“高”
}
怎么了?
可变内容应该是可变内容(注意下划线),并且应该与通知处于同一级别。
(这个我可能误解了,但是)FCM没有类别参数,click_action已经与之对应。
这里的参数见文档
在使用Firebase通知控制台时,当前无法设置单击操作和可变内容的值。您必须自己构建有效负载,如下所示:
{
"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"
}
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Some one invited you.",
"title": "Friend request notification",
"click_action" : "provider-body-panel"
}
}
{
“致”:“,
“可变内容”:正确,
“通知”:{
“身体”:“有人邀请你。”,
“标题”:“好友请求通知”,
单击“操作”:“提供程序正文面板”
}
}
然后从您自己的应用服务器发送。你也可以用邮递员或卷发来完成
这对我很有用,希望你也一样。首先,我要提到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"
}
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Some one invited you.",
"title": "Friend request notification",
"click_action" : "provider-body-panel"
}
}
{
“通知”:{
“身体”:“有人邀请你。”,
“标题”:“好友请求通知”,
“可变内容”:正确,
“图标”:“我的图标”,
“声音”:“三声”
},
“数据”:{
“类别”:“提供者主体面板”,
“可变内容”:正确,
单击“操作”:“提供程序正文面板”
}
}
“注册ID”:[“”],
“优先级”:“高”
}
怎么了?
可变内容应该是可变内容(注意下划线),并且应该与通知处于同一级别。
(这个我可能误解了,但是)FCM没有类别参数,click_action已经与之对应。
这里的参数见文档
在使用Firebase通知控制台时,当前无法设置单击操作和可变内容的值。您必须自己构建有效负载,如下所示:
{
"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"
}
{
"to": "<REGISTRATION_TOKEN_HERE>",
"mutable_content" : true,
"notification": {
"body" : "Some one invited you.",
"title": "Friend request notification",
"click_action" : "provider-body-panel"
}
}
{
“致”:“,
“可变内容”:正确,
“通知”:{
“身体”:“有人邀请你。”,
“标题”:“好友请求通知”,
单击“操作”:“提供程序正文面板”
}
}
然后从您自己的应用服务器发送。你也可以用邮递员或卷发来完成
这对我有用,希望你也一样。当你通过Firebase控制台发送通知时,你是否正确收到通知?是的,亲爱的,我已经通过Firebase控制台发送了通知,工作正常。你尝试过@Shubhank链接的内容吗?通过在有效负载中添加
priority
参数并将其设置为high
?是的,我还在有效负载中添加了priority字段。但仍然存在同样的问题。当您通过Firebase控制台发送通知时,您是否正确地收到了通知?是的,亲爱的,我已经通过Firebase控制台发送了通知,并且工作正常。您是否尝试了@Shubhank链接的内容?通过在有效负载中添加priority
参数并将其设置为high
?是的,我还在有效负载中添加了priority字段。但还是一样的问题。