Firebase自定义消息iOS

时间:2018-05-15 16:53:22

标签: ios swift firebase

我指的是https://firebase.google.com/docs/cloud-messaging/ios/receive中的口译讯息部分。

在我的代码中,我可以在Firebase中更改通知的文本吗?

2 个答案:

答案 0 :(得分:0)

为了向设备发送推送通知,您需要在服务器上理想地托管一个脚本(或一段代码),代表您发送推送通知。
在那里,您可以自定义消息,甚至在收到通知后播放音频 以下是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 Server上部署的Java应用程序中。

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

希望这有帮助

答案 1 :(得分:0)

首先创建p.12证书并在firebase中上传 - &gt;项目设置 - &gt;云消息传递选项卡 - &gt;选择您的iOS应用 - &gt;添加APNS证书。

一个。创建(.certSigningRequest)CSR文件

从公用事业打开钥匙串访问 从Keychain Access工具栏中选择Keychain Access - &gt;偏爱 在弹出窗口中,选择“证书”选项卡 将“在线证书状态协议”和“证书撤销列表”都设置为“关闭&#34; 关闭此窗口 现在从工具栏中,打开Keychain Access&gt;证书助理&gt;从证书颁发机构申请证书 输入您在iOS Developer Program中注册时使用的电子邮件地址和通用名称 将CA Email保留为空白,然后选择“保存到磁盘”和“让我指定密钥对信息” 单击继续 选择文件名&amp;目的地在你的硬盘上 单击保存 在下一个窗口中,将“密钥大小”值设置为“2048位” 将“算法”设置为“RSA” 单击继续 这将创建certSigningRequest文件(CSR)并将其保存到硬盘驱动器。还将在Keychain Access中创建公钥和私钥,并输入公用名。

B中。创建&#34; .cer&#34; iOS开发者帐户中的文件

登录Apple开发者帐户点击“证书,标识符&amp;配置文件” 点击“配置配置文件” 在“证书”部分中,单击“生产” 单击主面板右上角的“添加”(+)按钮 现在,选择“App Store和Ad Hoc” 单击继续 点击“选择文件”&amp;找到您从硬盘驱动器制作的CSR文件 单击“生成” 单击“下载”以获取该文件 C.安装.cer并生成.p12证书

找到您下载的.cer文件,然后双击 将登录下拉菜单设置为“登录&#34;并单击“添加” 打开KeyChain Access,您将在步骤A中找到创建的个人资料 您可以展开“私钥”配置文件(显示您添加的证书) 只选择这两项(不是公钥) 右键单击并从弹出窗口中单击“导出2个项目...” 现在确保文件格式为“.p12”并在硬盘上选择文件名和目标 单击保存。现在,系统会提示您设置密码,但请将这些密码保留为空白 单击确定。现在,您的硬盘上有一个.p12文件

并打开您的Xcode项目并选择target-&gt; capabilities-&gt; pusnotification-&gt; on

接下来做这些事https://firebase.google.com/docs/cloud-messaging/ios/receive

来自firebase云消息控制台的下一个推送消息,包含消息和标题,然后选择您的应用目标 - &gt;用户细分 - >您的应用。

然后你的应用就可以了