gpt4 book ai didi

ios - Firebase 自定义消息 iOS

转载 作者:行者123 更新时间:2023-11-28 07:48:15 25 4
gpt4 key购买 nike

我指的是 https://firebase.google.com/docs/cloud-messaging/ios/receive 中的解释消息部分.

我可以在代码的哪个位置更改 Firebase 中的通知文本?

最佳答案

为了向设备发送推送通知,您需要有一个脚本(或一段代码)理想地托管在可以代表您发送推送通知的服务器上。
您可以在那里自定义消息,甚至可以在收到通知时播放音频。
以下是 java 中的代码片段,可用于向设备(或一组设备)发送推送通知。

private Map sendPush(String to, String from, String title, String message,
String sound) throws IOException {
sound = (sound != null) ? sound : "default"; // set default audio file name
// Creating the URL and connection
URL url = new URL(FCM_URL); // your firebase URL
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + FCM_KEY); // the firebase project key

conn.setDoOutput(true);

// set the notification body
Map<String, String> notificationBody = new HashMap();
notificationBody.put("title", title); // notification title
notificationBody.put("body", message); // notification message
notificationBody.put("sound", sound);
notificationBody.put("badge", "1");

Map<String, String> dataBody = new HashMap();
dataBody.put("sender", from); // sender id

Map<String, Object> pushBody = new HashMap();
pushBody.put("notification", notificationBody);
pushBody.put("data", dataBody);
pushBody.put("to", to); // receiver(s) id
pushBody.put("priority", "high");

// convert your dictionary to json string using Google Gson library (similar to JsonSerialization class in swift)
String input = new Gson().toJson(pushBody);

// write input bytes in request body
try (OutputStream os = conn.getOutputStream()) {
os.write(input.getBytes());
os.flush();
}

StringBuilder responseString;
Reader reader = new InputStreamReader(conn.getInputStream()); // send request and receive response

// parse response
try (BufferedReader in = new BufferedReader(reader)) {
String inputLine;
responseString = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
responseString.append(inputLine);
}
}

// using Google Gson to convert json string into Map (similar to JsonSerialization class in swift)
Map<String, Object> responseObject = new Gson().fromJson(responseString.toString(),
Map.class);

return responseObject;
}

由于这是一个 java 代码,所以我将它托管在部署在 Apache Tomcat 服务器上的 java 应用程序中。

您可以在各种语言(如 php 或 node.js 等)中找到几个类似的实现。

希望对你有帮助

关于ios - Firebase 自定义消息 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50355533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com