I integrate Google Firebase Cloud Message in my application. When my app is in foreground or in background it can receive a message push from Google server, but if my app is dead, it can not receive any message push from google server. What I want is that if my app is dead it can still receives a message from google server. Here is my code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgServiceDemo";
@Override
public void onMessageReceived(RemoteMessage remotemsg) {
Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());
sendNotification(remotemsg.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Demo Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Logger.e("TESTTTTT111111111", token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
}
}
<service
android:name="com.linkus.fcm.MyFirebaseInstanceIDService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="com.linkus.fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>