FCM Notification not received (on Galaxy S8 and On

2019-03-06 06:06发布

I have implemented FCM (Firebase Notification) and everything is working fine on all devices except Samsung Galaxy S8 and One-Plus 3

When application running in foreground/background everything is working perfectly

But when I Remove the application from Task Manager (Swipe-Tray) or finish the application using backPress() devices stop receiving notification,

My complete code of service and manifest.

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>

My server side code :

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条回答
冷血范
2楼-- · 2019-03-06 06:27

FCM sends notification in two forms inside data tag and another one in notification tag. When the app is open the data is triggered and when the app is cleared from the background, FCM tries to handle the notification on its own using notification tag. What you can do is by asking the server guy only to send with data as a key and not notification. Then you can handle the notification.

For more information read this link

查看更多
登录 后发表回答