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>