Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open url in external browser on Tap on android push notifications?

I am trying to open the browser with a url when the user click on the push notification, i search in stackoverflow and i find this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);

but it doesnt work for me.
I am searching for the solution for last 5 days but failed to find the one.
Here is my code

public class MyFirebaseMessagingService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

// Intent notificationIntent = new Intent(this, HomeActivity.class);

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.example.android"));
startActivity(notificationIntent);


notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);

int notificationId = new Random().nextInt(60000);
Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("image-url"));

Intent likeIntent = new Intent(Intent.ACTION_VIEW);
likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
likeIntent.putExtra(IMAGE_URL_EXTRA,remoteMessage.getData().get("image-url"));
PendingIntent likePendingIntent = PendingIntent.getService(this,
notificationId+1,likeIntent, PendingIntent.FLAG_ONE_SHOT);


Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels();
}

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setLargeIcon(bitmap)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("title"))
.setStyle(new NotificationCompat.BigPictureStyle()
.setSummaryText(remoteMessage.getData().get("message"))
.bigPicture(bitmap))/*Notification with Image*/
.setContentText(remoteMessage.getData().get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.addAction(R.drawable.ic_favorite_true,
getString(R.string.notification_add_to_cart_button),likePendingIntent)
.setContentIntent(pendingIntent);

notificationManager.notify(notificationId, notificationBuilder.build());

}

Looking for any help..

like image 370
Gopal Meena Avatar asked Aug 05 '20 22:08

Gopal Meena


People also ask

How to open website URL in Android’s web browser from application?

Android application developer can insert an particular website URL into android activity on button click using Intent and when user clicks on button then it will automatically open given url into built in default mobile device web browser. So here is the complete step by step tutorial for Open Website Url in Android’s Web browser from application.

How to start an activity from a URL in Android?

You can pass android.net.Uri instance to the 2nd parameter, and a new Intent is created based on the given data url. And then, simply call startActivity (Intent intent) to start a new Activity, which is bundled with the Intent with the given URL. Do I need the if check statement? Yes. The docs says:

Do all devices need an application to handle a URL?

All devices should have an application to handle a URL. You can add your own failure/exception handling as needed, Agree with you. Just like the solutions other have written (that work fine), I would like to answer the same thing, but with a tip that I think most would prefer to use.

How to dispatch the URL in a mobile application in flutter?

In Flutter, everything is a widget and similarly, Flutter likewise utilizes a lot of plugins or dependencies to make the application work quicker and simpler. For this situation, the “url_launcher” plugin can be utilized to dispatch the URL in a mobile application.


1 Answers

Whatever you have tried so far is pretty correct. I've tried your code on a sample android app,

package com.example.notificationapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private static final String NOTIFICATION_CHANNEL_ID = "Notification Channel Name";
    private static final String NOTIFICATION_CHANNEL_NAME = "Notifications";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Intent notificationIntent = new Intent(this, HomeActivity.class);

        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.example.android"));
        startActivity(notificationIntent);

        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_ONE_SHOT);

        int notificationId = new Random().nextInt(60000);
//        Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("image-url"));

//        Intent likeIntent = new Intent(Intent.ACTION_VIEW);
//        likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
//        likeIntent.putExtra(IMAGE_URL_EXTRA, remoteMessage.getData().get("image-url"));
//        PendingIntent likePendingIntent = PendingIntent.getService(this,
//                notificationId+1,likeIntent, PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        //.setLargeIcon(bitmap)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("some random title")
//                        .setStyle(new NotificationCompat.BigPictureStyle()
//                                .setSummaryText("message from remote message")
//                                .bigPicture(bitmap))/*Notification with Image*/
                        .setContentText("Adding some text within content text")
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
//                        .addAction(R.drawable.ic_favorite_true,
//                                getString(R.string.notification_add_to_cart_button),likePendingIntent)
                        .setContentIntent(pendingIntent);

        notificationManager.notify(notificationId, notificationBuilder.build());
    }
}

This is what I see,

enter image description here

So I see the browser is getting opened with the url specified.

I highly suspect your onMessageReceived method within MyFirebaseMessagingService is not invoked. Please add a log message within onMessageReceived and see if you are getting invoked that can possibly be the next thing to do if you haven't tried already.

like image 169
Tom Taylor Avatar answered Sep 29 '22 13:09

Tom Taylor