1

I'm trying to open up a browser page to a specific url after the user opens an FCM Push Notification they've received. This is the code I currently have:

import android.content.Intent;
import android.net.Uri;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FMMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        startActivity(browserIntent);
    }
}

On tapping the push notification from the FCM console, nothing happens. I've messed around with this for a while, looking for solutions online, but nothing seems to work.

Please can someone help with this issue?

Take note, I am incredibly new to this and don't understand a whole lot.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Korallion
  • 13
  • 3

1 Answers1

0

You can use a Data notification with the Intent.ACTION_VIEW

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));

PendingIntent pending = PendingIntent.getActivity(this, 0, notificationIntent, 
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.setContentIntent(pending);

If the message has data, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity. According to the data provided, you can handle the condition appropriately.

Set click_action in the notification payload

{
  "notification": {
      "click_action": "activity action"
  },
 ....
}

In the mainfest you have to config the inter filter.

<intent-filter>
  <action android:name="some action" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

then when you click it, it will open that action activity: If specified, an activity with a matching intent filter is launched when a user clicks on the notification.

You can set the click_action acton to match the browser chooser action, or "Intent.ACTION_VIEW" and data set like that "https://"

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20