Home > Back-end >  Clicking on a notification received while the app is in the foreground does nothing
Clicking on a notification received while the app is in the foreground does nothing

Time:10-14

I hope Android experts know how to help me. I am working with Push Notifications (I send them from Postman using the FCM service), the point is that while the app is in Background everything works great(I mean that they are displayed correctly and redirect to the pages that I want), however When the app is in Foreground, they look excellent but when you touch them, they do absolutely nothing. I am very new to Android studio since it is a work project and I have been learning as I go, any help or explanation is very appreciated. I show you a bit of my code below.

This works fine when the app is in Backgroud

This shows me the notification when the app is in the foreground, but it does nothing when I click

EDIT: ACTUAL CODE

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: "   remoteMessage.getData().get("message"));
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        manager.notify(23, builder.build());
    }
}

However I think that part of the problem comes from the fact that this code block is never executed, but the strange thing is that the pushNotificationActionPerformed listener is executed

DON'T WORK:

PushNotifications.addListener(
      'pushNotificationReceived',
      (notification: PushNotificationSchema) => {
        alert('Llegó una notificación!!!');
        if (notification && Capacitor.getPlatform() === 'android') {
          this.createLocalNotification(notification);
        }
      }
    );

WORK:

 PushNotifications.addListener(
      'pushNotificationActionPerformed',
      (notification: ActionPerformed) => {
        alert('Llegó una notificación!!!');
        const data = notification.notification.data;
        if (data.ticket) {
          const ticketParsed = JSON.parse(data.ticket);
          this.store.dispatch(new StoreSelectedTicket(ticketParsed));
          this.router.navigate(['/support/ticket-status']);
        }
        if (data.statement) {
          const parsedStatement = JSON.parse(data.statement);
          this.downloadStatements(parsedStatement.link);
        }
        if (data.deposit) {
          const parsedDeposit = JSON.parse(data.deposit);
          this.store.dispatch(new StoreSelectedDeposit(parsedDeposit));
          this.router.navigate(['/deposits/daily-detail/detail-by-reference-number']);
        }
      }
    );
  }

CodePudding user response:

I guess following might be the reasons:

Firstly the flag for the intent which is passed in the pending intent has to be either Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

Secondly the last parameter for pendingIntent can be 0 if you don't need FLAG_ONE_SHOT for something specific.

Thirdly use a non zero unique integer for manager.notify(non-zero-integer,builder.build)

You can check more information in the official documentation

  • Related