使用Java发送Firebase通知,而不是使用FCM控制台

3

我正在给我的安卓应用添加通知功能,但是似乎无法通过我的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

有其他人遇到过这个问题吗?非常感谢您的帮助。

1个回答

4

你的代码对我来说可以运行。添加下面提到的行以查看完整的响应状态。发布输出。

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

        System.out.println("Resonse: " + response); // <= ADD THIS

此外,如果您希望Firebase为您生成通知,则需要将bodytitle作为notification的属性,而不是data的属性。详细信息请参阅文档
        info.put("title", "FCM Notification Title"); // Notification title
        info.put("body", "Hello First Test notification"); // Notification body
        data.put("notification", info); // <= changed from "data"

是的!它起作用了!把信息推送到通知中心中确实是有意义的。非常感谢你! - Pooshonk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接