I saw that this is not an uncommon topic, and I have tried to read other posts, but I don't know how to apply it to my app. In my app, I am sending Push Notifications through PostMan, which then opens different activities according to the flags I'm setting. I would like to send also different links to the notification so when the user clicks on it, it will open a browser. In other words, I would like to have both the activity flags when the notification is dedicated to an activity, and the URL, when the notification contains a specific link.
I have seen from the answers from a previous post that one solution would be so set an intent like this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.google.com"));
PendingIntent contentIntent = PendingIntent.getActivity(contexta, 0, notificationIntent, 0);
notification.setLatestEventInfo(contexta, contentTitle, contentText, contentIntent);
mNotificationManager.notify(970970, notification);
but as far as I can see, this has www.google.com set by default. How can I make it so that it opens the specific link I send by PostMan? I assume that I need to introduce the above code into my IF conditions somehow (as in here), but how?
The code that I have for the PushNotifications is given below:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";
public MyFirebaseMessagingService() {}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.d(TAG, "Message Notification click_action: " + click_action);
sendNotification(title, message, click_action);
}
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title, String messageBody, String click_action) {
Intent intent;
if (click_action.equals("EVENIMENTE")) {
intent = new Intent(this, Evenimente.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (click_action.equals("PRIMA_PAGINA")) {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
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)
.setSmallIcon(R.drawable.app_logo_final)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setLights(Color.RED, 3000, 3000)
.setSound(defaultSoundUri)
.setVibrate(new long[] {
1000,
1000,
1000,
1000,
1000
})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build());
}
}
So, I have the click_action
set, as was also suggested in some other posts so I guess this is where the link can be sent from PostMan, but how? Can anyone please help me with this? Thank you!