** Firebase FCM在关闭应用程序时通知,但在打开应用程序时可以正常工作我正在尝试使用Firebase中的FCM发送带有数据的通知,但我不能,在打开应用程序时,通知效果很好但在关闭时不起作用**
这是我用来发送通知的代码
public static void send(String title, String notificationMasseg, String token, String odrerID) {
String url = "https://fcm.googleapis.com/fcm/send";
JSONObject jsonObject = new JSONObject();
try {
// jsonObject.put("to", "/topics/all");
jsonObject.put("to", token);
jsonObject.put("collapse_key", "type_a");
jsonObject.put("priority", 10);
JSONObject data = new JSONObject();
data.put("body", notificationMasseg);
data.put("title", title);
data.put("order_id", odrerID);
data.put("request_type", "chat_massage");
jsonObject.put("data", data);
JSONObject android = new JSONObject();
android.put("priority","high");
jsonObject.put("android",android);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "key=AAAAY4PFnG4*******");
headers.put("Content-Type", "application/json");
return headers;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}
这就是服务
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "notification00";
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (remoteMessage.getData().size()>0){
//do some stuff
}
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "data: " + remoteMessage.getData());
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
}
}
**最后这是清单**
<service
android:name=".service.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/white" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
这就是一切,所以,如果我能找到任何人来帮助我,我会很感激