【问题标题】:Send Firebase Notification using Java, not FCM Console使用 Java,而不是 FCM 控制台发送 Firebase 通知
【发布时间】:2017-04-20 14:27:38
【问题描述】:

我正在向我的 android 应用程序添加通知,但我似乎无法通过我的 java 代码发送通知。我从响应中返回了 200 个 http 代码,但没有显示任何内容。

这适用于 Firebase 控制台本身,但是一旦我尝试使用我的 Java 代码,事情就会发生。

这是我下面的代码

package actions.authentication;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.opensymphony.xwork2.Action;

import edocs.actions.PublicAction;
import net.sf.json.JSONObject;

public class SendPushNotification extends PublicAction {

    /**
     * 
     */
    private static final long serialVersionUID = -8022560668279505764L;

    // Method to send Notifications from server to client end.
    public final static String AUTH_KEY_FCM = "SERVER_KEY_HERE";
    public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
    public final static String DEVICE_ID = "DEVICE_TOKEN_HERE";

    public String execute() {
        String DeviceIdKey = DEVICE_ID;
        String authKey = AUTH_KEY_FCM;
        String FMCurl = API_URL_FCM;

        try {
            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");
            System.out.println(DeviceIdKey);
            JSONObject data = new JSONObject();
            data.put("to", DeviceIdKey.trim());
            JSONObject info = new JSONObject();
            info.put("title", "FCM Notificatoin Title"); // Notification title
            info.put("body", "Hello First Test notification"); // Notification body
            data.put("data", info);
            System.out.println(data.toString());
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data.toString());
            wr.flush();
            wr.close();

            int responseCode = conn.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            return Action.SUCCESS;
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        return Action.SUCCESS;
    }
}

System.out.println("Response Code : " + responseCode); 行打印出以下内容

响应代码:200

还有其他人遇到过这个问题吗?任何帮助将不胜感激

【问题讨论】:

    标签: java firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    您的代码对我有用。添加下面提到的行以查看完整的响应状态。发布输出。

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
    
            System.out.println("Resonse: " + response); // <= ADD THIS
    

    此外,如果您希望 Firebase 为您生成通知,bodytitle 需要是 notification 的属性,而不是 data。详情在the documentation

            info.put("title", "FCM Notification Title"); // Notification title
            info.put("body", "Hello First Test notification"); // Notification body
            data.put("notification", info); // <= changed from "data"
    

    【讨论】:

    • 是的!有效!您必须将信息推送到通知中是有道理的。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 2018-11-13
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2019-06-13
    • 2017-11-23
    相关资源
    最近更新 更多