未使用自定义服务器显示Android Firebase通知
我正在尝试使用Firebase云消息向我的android设备发送通知。显然一切都好。我得到HTTP请求的答复:未使用自定义服务器显示Android Firebase通知,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我正在尝试使用Firebase云消息向我的android设备发送通知。显然一切都好。我得到HTTP请求的答复: Response Code : 200 {"message_id": **ID here** } 但我有一个安装了APK的模拟器,没有收到任何通知,即使应用程序在前台也没有 通过Firebase控制台发送消息时,一切正常 有人能帮我找出问题所在吗 import java.io.BufferedReader; import java.io.IOException; i
Response Code : 200
{"message_id": **ID here** }
但我有一个安装了APK的模拟器,没有收到任何通知,即使应用程序在前台也没有
通过Firebase控制台发送消息时,一切正常
有人能帮我找出问题所在吗
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
String apiKey = "AI...LoU"; //My key here
String out = "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\"}, \"to\":\"/topics/all\",\"data\": {\"key\": \"value\"}}";
URL url = new URL("https://fcm.googleapis.com/fcm/send");
String credentials = "key=" + apiKey;
//String basicAuth = "Basic " + new String(Base64.getEncoder().encodeToString(credentials.getBytes()));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Content-Type", "application/json" );
conn.setRequestProperty("Authorization", credentials);
conn.connect();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(out);
wr.flush();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
wr.close();
}
}
请确保设备已订阅相应的主题。在这种情况下,应该通过在客户端调用来订阅
all
。这可能是emulator中的网络问题。发送通知后,在emulator中打开飞机模式并关闭飞机模式,您将收到所有通知。我在使用Firebase控制台时收到通知。我认为网络运行良好。使用此http post请求时,我没有收到通知。您确定设备(仿真器)订阅了all
主题吗?不!这可能是我的错误,@AL。我以为每台设备都会默认订阅“所有”主题(我在某处读到过)。注意到。您只需将设备订阅到该主题即可。我会补充一个答案。:)只是补充!当我的应用程序被创建时,在launcher活动的onCreate方法中,我添加了以下行:FirebaseMessaging.getInstance().subscribeToTopic(“全部”);现在它工作得很好。