简体   繁体   中英

Open url in browser instead of activity when Firebase notification is tapped - Android

I use this code (see below #The Code that I use ) working to open specific activity when Firebase Notification is tapped. Instead of opening specific activity, I want the Firebase Notification to open browser and go to that specific link.

I use Firebase Console in sending notification. I want to use this custom data.

Key: url
Value: www.randomurl.com

Please see the code below and help me achieve opening url in browser instead of opening my App.

#The Code that I use.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

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

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    String message = remoteMessage.getData().get("message");

    String imageUri = remoteMessage.getData().get("image");

    String TrueOrFalse = remoteMessage.getData().get("JournalActivity");

    //To get a Bitmap image from the URL received
    bitmap = getBitmapfromUrl(imageUri);

    sendNotification(message, bitmap, TrueOrFalse);
}

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("JournalActivity", TrueOrFalse);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.drawable.app_logo_new)
            .setContentTitle(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

public Bitmap getBitmapfromUrl(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        return bitmap;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}

}

Thanks!

In your pendingIntent,pass this intent.

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

First, you need to pass the url into your sendNotification method. To do this, add the following line in OnMessageReceived under String Message:

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

Then, pass url to your sendNotification method. Add url to the last line in onMessageReceived:

sendNotification(message, bitmap, TrueOrFalse, url);

Next, accept the url in your sendNotification method. Add url to your method header like this:

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse, String url)

Finally, add this under your intent in sendNotification:

intent.setData(Uri.parse(url));

This will cause the notification to open your link in the browser when it is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM