0

When app is running and notification is alert if I click it, notification is go to there (onMessageReceived is called)

But when app is in background and notification is alert if I click it, onMessageReceived not called

How to fix it?

ps. sorry my english is not good. My code

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    String fnotification="0";
    String post_id;
    String bage;
    String notification_id;
    String type;
    String message;

    private static final String TAG = "MyFirebaseMsgService";
    RemoteMessage remoteMessage;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "onMessageReceived: ");
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();
        Log.e("FROM", remoteMessage.getFrom());
        post_id = data.get("post_id");
        bage = data.get("badge");
        type = data.get("type");
        message = data.get("message");
        notification_id = data.get("notification_id");

        Log.d("MyFirebaseMessagingServ","data=>"+data);
        Log.d("MyFirebaseMessagingServ","notification=>"+notification);
        sendNotification(notification, data);
        //push_token
    }

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);



    }

    private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            int int_badge_count = Integer.parseInt(bage);
            ShortcutBadger.with(getApplicationContext()).count(int_badge_count);
        } catch (Exception e) {

        }


        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_android);


        intent.putExtra("menuFragment", fnotification.toString());
        intent.putExtra("badges_count", bage);
        intent.putExtra("post_id",post_id);
        intent.putExtra("type",type);
        intent.putExtra("message",message);
        intent.putExtra("notification_id",notification_id);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                //.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.win))
                .setContentIntent(pendingIntent)
               // .setContentInfo("Hello")
                .setLargeIcon(icon)
                //.setColor(Color.RED)
//                .setLights(Color.RED, 1000, 300)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(getNotificationIcon());
//                .setSmallIcon(R.drawable.icon_android);

      /*  try {
            String picture_url = data.get("picture_url");
            if (picture_url != null && !"".equals(picture_url)) {
                URL url = new URL(picture_url);
                Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                notificationBuilder.setStyle(
                        new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
        sendNoti(this);

    }

    private void sendNoti(Context context){
        Intent intent = new Intent("push");
        intent.putExtra("menuFragment",fnotification.toString());
        intent.putExtra("post_id", post_id);
        intent.putExtra("badges_count",bage);
        intent.putExtra("type",type);
        intent.putExtra("notification_id",notification_id);
        intent.putExtra("message",message);
        context.sendBroadcast(intent);
    }

    private int getNotificationIcon() {
        Log.d(TAG, "getNotificationIcon: ");
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.ic_notifications_white_24dp : R.drawable.icon_android;
//        return useWhiteIcon ? R.drawable.btn_menu_notification_1 : R.drawable.ic_launcher;

    }
AL.
  • 36,815
  • 10
  • 142
  • 281
ARR.s
  • 769
  • 4
  • 20
  • 39
  • You have to include `data payload` to trigger `onMessageReceived` and rise notification if you want to do that when app is in background, and for that you need custom server side logic as that is not possible from firebase console but luckly for you there are tutorials how to do that – Yupi Jun 28 '17 at 09:39
  • Can you give some example for me? – ARR.s Jun 28 '17 at 09:46
  • This tutorial will work for you: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ And here you have custom made php server from where you can send notfications: demo.androidhive.info/firebase/notifications – Yupi Jun 28 '17 at 09:49
  • See my answer to the same question here: https://stackoverflow.com/questions/44643866/firebase-notification-not-working-in-background/44879663#44879663 – Diego Giorgini Jul 03 '17 at 19:10

0 Answers0