2

I am getting push notification when application in foreground/ background/ killed but onMessageReceived not called when application Kill. I write a code to play custom notification sound in onMessageReceived method continouesly. Custom notification sound also play once when application Killed. We are passing Data message and Notification message from cloud server(FCM). I appreciate the help.

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMessagingServ";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("@@", ">>>>>>>>>: Inside CLASS");
    Log.e("@@", ">>>>>>>>>: Inside ON MESSAGE RECEIVED");
    Map<String, String> data = remoteMessage.getData();
    String orderID = data.get("orderId");
    String orderType = data.get("orderType");
    String orderStatus = data.get("orderStatus");
    Intent intentService = new Intent(getApplicationContext(), SoundIntentService.class);
    startService(intentService);
}
} 

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();



        // sending reg id to your server
        sendRegistrationToServer(refreshedToken);

        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
        registrationComplete.putExtra("token", refreshedToken);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void sendRegistrationToServer(final String token) {
        // sending gcm token to server
        Log.e(TAG, "sendRegistrationToServer: " + token);
    }


}

Manifest.xml

<service
            android:name=".service.SoundIntentService"
            android:exported="false" />
<service android:name=".service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
smpatel
  • 71
  • 1
  • 8
  • Try this: https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background – Khyati Chitroda Jun 01 '18 at 13:11
  • 1
    BTW:: There is no need to store the `Token` in a `SharedPreferences` like in your method `storeRegIdInPref()` because you can get the `Token` anywhere in your app using `String myToken = FirebaseInstanceId.getInstance().getToken();` – Barns Jun 01 '18 at 13:38
  • What payload are you using while application is killed – Rohit Sharma Jun 02 '18 at 05:39
  • we have passed notification and data both payload using cloud function. @RohitSharma – smpatel Jun 04 '18 at 10:27
  • You should use data when app is killed and you have to send it manually from post man – Rohit Sharma Jun 04 '18 at 15:26
  • Refer to this article https://www.google.co.in/url?sa=t&source=web&rct=j&url=https://medium.com/android-school/test-fcm-notification-with-postman-f91ba08aacc3&ved=2ahUKEwjCipnbqrrbAhXCfCsKHeBGAegQFjACegQIAxAB&usg=AOvVaw1SurJsGDsbgklzTWdZEtLg – Rohit Sharma Jun 04 '18 at 15:37
  • I works as charm.data message solved everything in pushnotification. Thanks @Rohit Sharma – smpatel Jun 05 '18 at 13:22

0 Answers0