2

I am receiving the notification when the app is in foreground and background. But when I close the app no notification is received.

This is my manifest declaration

        <service android:name="com.vfi.fcm.AppInstanceIDListenerService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android:name="com.vfi.fcm.AppFcmListenerService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

This is the FCM listener class

public class AppFcmListenerService extends FirebaseMessagingService {

private static final String TAG = "FCM - Msg";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Map<String, String> data = remoteMessage.getData();
    Set keys = data.keySet();

    for (Iterator i = keys.iterator(); i.hasNext(); ) {
        String key = (String) i.next();
        Log.e("key",key);
        String value = (String) data.get(key);
        sendNotification(null, value);
        Log.e("value",value);
    }

}

private void sendNotification(String type, String message) {
    Random random = new Random();
    int Unique_Integer_Number = random.nextInt(9999 - 1000) + 1000;
    Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            getApplicationContext(), Unique_Integer_Number, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    builder.setContentTitle(AppControl.getInstance().getAppName());
    builder.setContentText(message);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setSound(defaultSoundUri);
    builder.setAutoCancel(true);
    builder.setContentIntent(pendingIntent);
    builder.setStyle(new NotificationCompat.BigTextStyle()
            .bigText(message));
    nManager.notify(AppControl.getInstance().getAppName(), Unique_Integer_Number, builder.build());
}

I am testing it on Leeco 2 device. Notification is not received when app is closed.

I have tried the solutions available on this Firebase background but none of it solves my issue.

This is my payload

{
"data": {
"Message": "great match!"
},
"to": "fcm-id"
}
Community
  • 1
  • 1
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • 1
    When your app is in the background, Android directs notification messages to the system tray. So onMessageReceived is not called. Check this out. http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – mertsimsek Mar 09 '17 at 12:32
  • If you want to handle the notification in background then ask server guyz to remove notification object from payload of PUSH and every time you will get the notification call in onMessageReceive even app is in background. But in such case you need to generate the notification in system tray by your self. – VikasGoyal Mar 10 '17 at 05:42
  • @Avi I am using the data object not notificaiton object – WISHY Mar 10 '17 at 06:15
  • If you are only providing the data part in notification payload then it will give you call in the background too. Please refer here(https://firebase.google.com/docs/cloud-messaging/android/receive). – VikasGoyal Mar 10 '17 at 07:01

0 Answers0