0

FCM Service. NotificationDatabaseHandler is helper class. Saving message title and current time.

public class ApplicationFCMService extends FirebaseMessagingService {

    private static final String TAG = ApplicationFCMService.class.getName();
    private NotificationUtils notificationUtils;
    private NotificationDatabaseHandler databaseHandler;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.e(TAG, "From: " + remoteMessage.getFrom());
        databaseHandler = new NotificationDatabaseHandler(getApplicationContext());

        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());

            databaseHandler.addNotification(remoteMessage.getNotification().getBody(), getDate());
            //  Log.d("FCM", messagesSet.toString());
            Log.d("FCM", remoteMessage.getNotification().getBody());
        }

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Sanket Naik
  • 176
  • 2
  • 8
  • check this ans : https://stackoverflow.com/questions/45875859/fcm-onmessagereceived-not-calling-android/45880920#45880920 – Maddy Sep 16 '17 at 10:34
  • https://stackoverflow.com/questions/45267335/create-an-repetitive-high-pitch-alarm-on-a-remote-trigger-when-app-is-not-runn/45406863#45406863 – Don Chakkappan Sep 16 '17 at 10:35
  • Is there any way to send notifications as "Data" from Firebase? – Sanket Naik Sep 17 '17 at 13:46

1 Answers1

0

I've used postman to send data notifications(for testing). On reception of data message onMessageReceived() method is executed. Now I can save notifications in database even if the app is in background.

Sanket Naik
  • 176
  • 2
  • 8