0

Im trying to make my app open a link in the browser when a notification is recieved, but cant find a method that works with the app in foreground and in background.

The notification looks like this

  "notification": {
    "title": "My app try",
    "body": "This is a notification, YAY!",
  },
  "data": {
    "code":"1003",
    "url":"https://www.thisishard.com"
  }

If the app is closed I need to show the notification and when the user clicks on it, open the app and then open the browser.

But if the app is opened, I need to show the notification and only open the browser if the user clicks it.

Now it just opens the browser no metters what if the app is open and just opens the app if it is closed.

Im using firebase cloud messaging to send the notifications.

EDIT:

What I do is, in the onMessageReceived(), use an Intent to open the web browser

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String intentUri = remoteMessage.getData().get("url");

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUri));
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(browserIntent);
    }

1 Answers1

0

You must create own notification's onClick listener method like this

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse("http://www.google.com")); 
        PendingIntent pi = PendingIntent.getActivity(context, 0, notificationIntent, 0);
         // Resources r = getResources();
          Notification notification = new NotificationCompat.Builder(context)
                  .setTicker("yortext")
                  .setSmallIcon(android.R.drawable.ic_menu_report_image)
                  .setContentTitle("yortext")
                  .setContentText("sdsd")
                  .setContentIntent(pi)
                  .setAutoCancel(true)
                  .build();

          NotificationManager notificationManager2 =  (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
          notificationManager2.notify(0, notification);

Here is a good example

BekaKK
  • 2,173
  • 6
  • 42
  • 80
  • Question. If i do this when the app is in foreground, the notification created by te OS will open the app, and then the app will create a new notification? – Martín Unzueta Feb 19 '18 at 20:43
  • I think yes.You can open url as soon as you receive new notification. Main idea is to test this code when app is foreground – BekaKK Feb 19 '18 at 20:46