根据https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
,我可以使用数据消息,它将在MessageReceived方法上100%接收并采取相应的行动。但就我而言,当应用程序被杀死时,我的任何消息都没有收到。尽管有时仅在我在 Nexus 5 上进行测试时会收到它们。下面是我的数据有效载荷。
05-16 06:46:01.663 13199-29574/jss.test E/MyFirebaseMsgService: Data Payload: {data={"image":null,"title":"My message"}}
05-16 06:46:01.667 13199-29574/jss.test E/Providerfalse: true
05-16 06:46:01.667 13199-29574/jss.test E/MyFirebaseMsgService: Notification JSON {"data":{"image":null,"title":"My message"}}
FCM 的清单如下:
<service
android:name=".FirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
火碱信息服务:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
//code to call service if message has specific data values.
}
catch (Exception e) {
e.printStackTrace(); }}
你能指导我在哪里修复以使其完美运行吗?
根据我在 fcm 实现中的测试,如果我在数据和通知密钥中传递数据,那么我只能在运行其他智能的应用程序时在 onMessageReceived 方法中获取通知,我无法收到通知,如果我只使用数据密钥发送通知,那么它在两种情况下都有效,因此,如果您的通知数据中存在通知密钥,请删除通知密钥并对其进行测试,它可能有效。
尝试以下方法,它会帮助你
private void sendNotification(String text, String senderId, String receiverId) {
HttpsURLConnection conHttp = null;
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
conHttp = (HttpsURLConnection) url.openConnection();
conHttp.setDoOutput(true);
conHttp.setDoInput(true);
conHttp.setRequestMethod("POST");
conHttp.setRequestProperty("Content-Type", "application/json");
//Put below you FCM API Key instead
conHttp.setRequestProperty("Authorization", "key="
+ “YOUR_FCM_API_KEY”);
JSONObject root = new JSONObject();
JSONObject data = new JSONObject();
data.put(KEY_FCM_TEXT, text);
data.put(KEY_FCM_SENDER_ID, sender);
root.put("data", data);
root.put("to", "/topics/user_" + receiverId);
byte[] outputBytes = root.toString().getBytes("UTF-8");
OutputStream os = conHttp.getOutputStream();
os.write(outputBytes);
os.flush();
os.close();
conHttp.getInputStream(); //do not remove this line. request will not work without it gg
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (conHttp != null) conHttp.disconnect();
}
}
在服务清单中再定义一件事:
android:stopWithTask="false"
它应该是工作,否则没有方法在应用程序被杀死时从onMessageReceived获取消息。
尝试使用 getBody(( 而不是 getData((:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getBody().toString.length() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getBody().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getBody().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
}
catch (Exception e) {
e.printStackTrace(); }}
public class MyService extends Service {
public MyService() {
}
public int onStartCommand(Intent intent, int flags, int startId) {
// Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
startService(new Intent(getApplicationContext(), FirebaseMessagingService.class));
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}