Abhi Sh
Abhi Sh

Reputation: 31

How to open url by clicking notification in android

##### when i click on notification the app reopens, i want to open a specific url after clicking the notification. #####

![https://drive.google.com/open?id=1YIoLK3039IbtswnSdc5k7TKbp6UPhWKf] [see the image here]1

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent iA = new Intent(this, MainActivity.class);
        iA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,iA,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("FCM NOTIFICATION");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());

    }
}

Upvotes: 0

Views: 3655

Answers (2)

devio
devio

Reputation: 654

You can use a method like this one and just pass arguments

    void openUrl(@NonNull RemoteMessage remoteMessage, Context context, String title, String body, String commingUrl){
    Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(commingUrl));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,CHANNEL_ID)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
}

and call the method inside onMessageReceived method ;like this just change with your variables

String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
    String text = remoteMessage.getNotification().getBody();
    String dataKey = remoteMessage.getData().get("title");                      
openAnotherApp(remoteMessage,getApplicationContext(),title,text, 
   remoteMessage.getData().get("title"));

Upvotes: 0

Asif Rahman
Asif Rahman

Reputation: 191

You can use pending intent like this in your notification:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Upvotes: 3

Related Questions