【发布时间】:2018-05-15 16:53:21
【问题描述】:
我指的是https://firebase.google.com/docs/cloud-messaging/ios/receive 中的解释消息部分。
我可以在我的代码中的什么位置更改 Firebase 中的通知文本?
【问题讨论】:
-
请先查看stackoverflow.com/help/how-to-ask,然后提供更多详细信息和代码来展示您已经尝试过的内容。
我指的是https://firebase.google.com/docs/cloud-messaging/ios/receive 中的解释消息部分。
我可以在我的代码中的什么位置更改 Firebase 中的通知文本?
【问题讨论】:
为了向设备发送推送通知,您需要有一个脚本(或一段代码)最好托管在可以代表您发送推送通知的服务器上。
在那里,您可以自定义消息,甚至在收到通知时播放音频。
这是java 中的代码 sn-p,可用于向设备(或一组设备)发送推送通知。
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 等。
希望对你有帮助
【讨论】:
http 请求。或者您可以探索 Google Cloud Functions。 firebase.google.com/docs/functions
首先创建 p.12 证书并在 firebase 中上传 -> 项目设置 -> 云消息选项卡 -> 选择您的 iOS 应用 -> 添加 APNS 证书。
A.创建 (.certSigningRequest) CSR 文件
从实用程序打开钥匙串访问 从 Keychain Access 工具栏中选择 Keychain Access -> Preference 在弹出窗口中选择证书选项卡 将“在线证书状态协议”和“证书撤销列表”都设置为“关闭” 关闭此窗口 现在从工具栏中,打开 Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority 输入您在 iOS 开发者计划中注册时使用的电子邮件地址和常用名 保留 CA 电子邮件空白并选择“保存到磁盘”和“让我指定密钥对信息” 点击继续 选择硬盘驱动器上的文件名和目的地 点击保存 在下一个窗口中,将“密钥大小”值设置为“2048 位” 将“算法”设置为“RSA” 点击继续 这将创建您的 certSigningRequest 文件 (CSR) 并将其保存到您的硬盘。还将在 Keychain Access 中创建公钥和私钥,并输入通用名称。
B.在 iOS 开发者帐号中创建“.cer”文件
登录苹果开发者账户点击“证书、标识符和配置文件” 单击“配置文件” 在“证书”部分中,单击“生产” 单击主面板右上角的“添加”(+) 按钮 现在,选择“App Store and Ad Hoc” 点击继续 单击“选择文件”并找到您从硬盘驱动器制作的 CSR 文件 点击生成 点击下载获取文件 C.安装.cer并生成.p12证书
找到您下载的 .cer 文件并双击 将登录下拉菜单设置为“登录”,然后单击添加 打开 KeyChain Access,您将找到在步骤 A 中创建的配置文件 您可以展开“私钥”配置文件(显示您添加的证书) 只选择这两项(不是公钥) 右键单击并从弹出窗口中单击“导出 2 项...” 现在确保文件格式为“.p12”并选择硬盘驱动器上的文件名和目标 单击保存。现在,系统会提示您设置密码,但将两者都保留为空白 单击确定。现在,您的硬盘上有一个 .p12 文件
然后打开你的Xcode项目并选择target->capabilities->pusnotification->on
接下来做这件事https://firebase.google.com/docs/cloud-messaging/ios/receive
来自firebase云消息控制台的下一条推送消息,带有消息和标题,然后选择您的应用目标->用户段->您的应用。
那么您的应用将能够
【讨论】: