Reputation: 654
I had that method to open URI from notification but it works only when the app is on foreground. What I can add to code to make the notification open URI even if the app is in the background? This method is called in class PushNotificationService extends FirebaseMessagingService
void openAnotherApp(@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(context, 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());
}
Upvotes: 1
Views: 454
Reputation: 654
in your MyFirebaseMessagingService class that extends FirebaseMessagingService class use this method
private void sendNotification(String messageTitle, String messageBody,String key,String value) {
//'MainActivity' is the target activity. When notification will be clicked, 'MainActivity' will be triggered
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (value!= null) {
intent.putExtra(key, value);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
messageTitle,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
and call it in the same class in onMessageReceived method and as arguments give it
String title = "";
String message = "";
String dataKey = remoteMessage.getData().get("openApp");
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
try {
title = remoteMessage.getNotification().getTitle();
} catch (Exception e) {
e.printStackTrace();
}
try {
message = remoteMessage.getNotification().getBody();
} catch (Exception e) {
e.printStackTrace();
}
}
sendNotification(title, message,"openApp",dataKey);
and finnelly use this code on your main activity
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey("openApp")) {
String url = bundle.getString("openApp");
System.out.println(url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
And now if you send from Firebase a link like data with the key "open App" the notification that appear when you click it it will open main activity then it will open the link.
Upvotes: 1