FCM push notifications will not let me in when they arrive with the app closed

1

The problem I have with notifications is that when the app is closed or in the background, I touch the notification and do nothing. That is to say, I receive notifications at all times, only when they arrive in 2nd plane, the app does not take me.

This is the code I use:

public class MyFirebaseMessagingService extends FirebaseMessagingService 
 implements FirebaseMessagingServices {

    public static String TAG = "MyFirebaseMsgService";


    @Override
    public void handleIntent(Intent intent) {
        Log.d(TAG, "From: handleIntent");
        if (!DataUser.getInstance().userId.equals("-1")) {
            sendNotification(Objects.requireNonNull(intent.getExtras()));
        }
    }

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.i(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.i(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.i(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(Objects.requireNonNull(remoteMessage.toIntent().getExtras()));
        }
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }

    private void sendNotification(Bundle bundle) {

        if (bundle.containsKey("Type")) return;

        if (ChatDataInstance.INSTANCE.getChatConversationFragmentVisible() != null
                && bundle.containsKey("data_message_creator_id")) {
            if (bundle.get("data_message_creator_id").equals(ChatDataInstance.INSTANCE.getChatConversationFragmentVisible().getAdapter().getUser_id())) {
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(50);
                ChatDataInstance.INSTANCE.getChatConversationFragmentVisible().getAdapter().newMessages();

                if (Foreground.INSTANCE.isForeground()) {
                    return;
                }
            }
        } else if (ChatDataInstance.INSTANCE.getChatActivityVisible() != null) {
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            assert v != null;
            v.vibrate(50);
            int position = ChatDataInstance.INSTANCE.getChatActivityVisible().getMViewPager().getCurrentItem();
            if (position != 0) {
                ChatDataInstance.INSTANCE.getChatActivityVisible().consultMessages(position);
            }
            if (Foreground.INSTANCE.isForeground())
                return;
        }

        int id_notification;
        switch (bundle.getString("data_type", "null")) {
            case "3":
                id_notification = Integer.parseInt(bundle.getString("data_type", "0")
                        + "0" +
                        bundle.getString("data_message_creator_id", "0")
                );
                break;
            case "2":
                id_notification = Integer.parseInt(bundle.getString("data_type", "0")
                        + "0" +
                        bundle.getString("data_creator_id", "0")
                );
                break;
            default:
                id_notification = Integer.parseInt(bundle.getString("data_type", "0"));
                break;
        }

        Intent resultIntent = new Intent(this, InitActivity.class);
        resultIntent.putExtras(bundle);
        // Adds the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(InitActivity.class);
        // Adds the Intent to the top of the stack
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        // Gets a PendingIntent containing the entire back stack
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        DataManager.getInstance().contpush = DataManager.getInstance().contpush + 1;

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                getApplicationContext(), bundle.getString("gcm.notification.android_channel_id"))
                .setContentIntent(resultPendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentTitle(bundle.getString("gcm.notification.title"))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(bundle.getString("gcm.notification.body")))
                .setContentText(bundle.getString("gcm.notification.body"))
                .setAutoCancel(true);

        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        assert v != null;
        v.vibrate(200);

        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(bundle.getString("gcm.notification.android_channel_id"),
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager.createNotificationChannel(channel);
        }
        mNotificationManager.notify(id_notification, mBuilder.build());

        try {
            Uri notification = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
                    notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
asked by Anderson Montilva 18.12.2018 в 22:14
source

1 answer

0

The PendingIntent that you create is not defining open some Activity , in this case I think it should be InitActivity .

    Intent resultIntent = new Intent(this, InitActivity.class);
    resultIntent.putExtras(bundle);
    // Adds the back stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(InitActivity.class);
    // Adds the Intent to the top of the stack
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

This is a way to define open a Activity in PendingIntent according to your code:

//define intent y Activity a abrir.   
Intent resultIntent = new Intent(this, InitActivity.class);
//Agrega extras
resultIntent.putExtras(bundle);
//Define flags para abrir Activity.
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//Define intent en PendingIntent
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, msgId , resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The value of msgId must be an integer value that is different for each notification as it will determine that the content is different between each notification.

    
answered by 18.12.2018 в 23:47