Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Notification not working in background

I need help. Firebase Notifications is Not Working in Background. This is My Code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "FROM:" + remoteMessage.getFrom());
   sharedPreference = getSharedPreferences(Global.SECURETRADE, 0);
    UID = sharedPreference.getString(Global.ID, "");


        Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`;

        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);

        } else {

            notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
        }

        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon));
        notificationBuilder.setContentTitle(remoteMessage.getData().get("title"));
        notificationBuilder.setContentText(remoteMessage.getData().get("body"));
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

    }




}
like image 683
Krishna Avatar asked Oct 29 '22 06:10

Krishna


2 Answers

Just remove 'notification' section from the json you sent through push notification. Simply sent the 'data' section, onMessageReceived will work as normal

like image 148
Angeo Johny M Avatar answered Nov 16 '22 13:11

Angeo Johny M


when app is in background or killed you have to use data payload for notification. Firebase onMessageReceived not called when app in background

like image 25
AAA Avatar answered Nov 16 '22 13:11

AAA