如果应用程序已关闭或从任务管理器中删除,则未收到FCM通知(在Galaxy S8和One-Plus 3上)

时间:2017-07-25 07:18:35

标签: android firebase notifications android-manifest firebase-cloud-messaging

我已经实施了FCM(Firebase通知),除三星Galaxy S8和One-Plus 3外,所有设备上的一切正常运行

当应用程序在前台/后台运行时,一切都运行良好

但是当我从任务管理器(Swipe-Tray)中删除应用程序或使用backPress()设备完成应用程序时停止接收通知,

我的完整服务和清单代码。

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        if (!remoteMessage.getData().containsKey("subtitle")) {
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                String title = remoteMessage.getData().get("title");
                String message = remoteMessage.getData().get("text");
                String rideId = remoteMessage.getData().get("ride_id");
                String rideName = remoteMessage.getData().get("ride_name");
                String ridePic = remoteMessage.getData().get("ride_pic");

                MessageDTO messageDTO = new MessageDTO();
                messageDTO.setRideId(Integer.parseInt(rideId));
                messageDTO.setRideName(rideName);
                messageDTO.setRidePic(ridePic);
                messageDTO.setMessage(message);
                // Don't show notification if chat activity is open.
                if (!AppController.isChatActivityOpen()) {
                    sendNotification(title, messageDTO);
                } else {
                    //EventBus.getDefault().post(new PushNotificationEvent(title, message, username, uid, fcmToken));
                }
            }
        }
        // Check if message contains a data payload.
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     */
    private boolean sendNotification(String title, MessageDTO messageDTO) {
        int id = (int) System.currentTimeMillis();
        Intent intent = new Intent(this, GroupChatActivity.class);
        intent.putExtra("SelectedChannel", messageDTO);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageDTO.getMessage())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, notificationBuilder.build());
        int returnValue = 0;

        return returnValue == 1 ? true : false;

    }

}

的AndroidManifest.xml

<service
    android:name="main.cyclesoon.com.chat.fcm.MyFirebaseMessagingService" android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</service>

我的服务器端代码:

public class FcmNotificationBuilder {
    public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
    private static final String TAG = "FcmNotificationBuilder";
    private static final String SERVER_API_KEY = "server_key";
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String APPLICATION_JSON = "application/json";
    private static final String AUTHORIZATION = "Authorization";
    private static final String AUTH_KEY = "key=" + SERVER_API_KEY;
    private static final String FCM_URL = "https://fcm.googleapis.com/fcm/send";
    // json related keys
    //private static final String KEY_TO = "to";
    private static final String KEY_TO = "registration_ids";

    private static final String KEY_TITLE = "title";
    private static final String KEY_TEXT = "text";
    //private static final String KEY_DATA = "data";
    private static final String KEY_DATA = "notification";
    private static final String KEY_RIDE_ID = "ride_id";
    private static final String KEY_RIDE_NAME = "ride_name";
    private static final String KEY_RIDE_PIC = "ride_pic";

    private String mTitle;
    private String mMessage;
    private JSONArray mReceiverFirebaseToken;
    private String mRideId;
    private String mRideName;
    private String mRidePic;

    private FcmNotificationBuilder() {
    }

    public static FcmNotificationBuilder initialize() {
        return new FcmNotificationBuilder();
    }

    public FcmNotificationBuilder title(String title) {
        mTitle = title;
        return this;
    }

    public FcmNotificationBuilder message(String message) {
        mMessage = message;
        return this;
    }

    public FcmNotificationBuilder setRideInfo(String rideId, String rideName, String ridePic) {
        mRideId = rideId;
        mRideName = rideName;
        mRidePic = ridePic;
        return this;
    }

    public FcmNotificationBuilder receiverFirebaseToken(JSONArray receiverFirebaseToken) {
        mReceiverFirebaseToken = receiverFirebaseToken;
        return this;
    }

    public void send() {
        RequestBody requestBody = null;
        try {
            requestBody = RequestBody.create(MEDIA_TYPE_JSON, getValidJsonBody().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Request request = new Request.Builder()
                .addHeader(CONTENT_TYPE, APPLICATION_JSON)
                .addHeader(AUTHORIZATION, AUTH_KEY)
                .url(FCM_URL)
                .post(requestBody)
                .build();

        Call call = new OkHttpClient().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
               Log.d(TAG, "onGetAllUsersFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse: " + response.body().string());
            }
        });
    }

    private JSONObject getValidJsonBody() throws JSONException {
        JSONObject jsonObjectBody = new JSONObject();
        ///JSONArray jsonArray = new JSONArray();
        //jsonArray.put(mReceiverFirebaseToken);
        //jsonArray.put("fPMLS022iPE:APA91bEBJjgUDN-mSPtZjS4EtWT_DGnTmajc8gaWJUjULjodPrq9OUvhSHc3Lkk1Mxx8Z_b6g6NpJhKqe0WxZETz6vpnAxL2UnYU5qZYM2u0YEZdF0JgX0bD0jViqi6guKmwE_rss2CK");
        //jsonObjectBody.put(KEY_TO, mReceiverFirebaseToken);
        jsonObjectBody.put(KEY_TO, mReceiverFirebaseToken);
        JSONObject jsonObjectData = new JSONObject();
        jsonObjectData.put(KEY_TITLE, mTitle);
        jsonObjectData.put(KEY_TEXT, mMessage);
        jsonObjectData.put(KEY_RIDE_ID, mRideId);
        jsonObjectData.put(KEY_RIDE_NAME, mRideName);
        jsonObjectData.put(KEY_RIDE_PIC, mRidePic);
        jsonObjectBody.put(KEY_DATA, jsonObjectData);

        return jsonObjectBody;
    }
}

1 个答案:

答案 0 :(得分:2)

FCM在数据标记内以两种形式发送通知,在通知标记中发送另一种形式的通知。当应用程序打开时,会触发数据,当应用程序从后台清除时,FCM会尝试使用通知标记自行处理通知。你可以做的是要求服务器人员只发送数据作为密钥而不是通知。然后你可以处理通知。

有关详细信息,请阅读此link