When I clicked notification run in background it's always call MainActivity , I sent url from Firebase Cloud Messing when I click when I'm in application it can open WebView that I create but if I clicked when close application it's always call MainActivity. No have any error and I think it not call any if-clause . please help and sorry for my english .
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() <= 0){
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
MessageHelper.displayMessage(getApplicationContext(), title, body);
}
if(remoteMessage.getData().size() > 0){
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String url = remoteMessage.getData().get("url");
MessageHelper.displayNotification(getApplicationContext(), title, body, url);
}
}
}
public class MessageHelper {
public static void displayMessage(Context context, String title, String body){
Intent intent = new Intent(context, NewsFragment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(0, builder.build());
}
public static void displayNotification(Context context, String title, String body, String url){
Intent intent = new Intent(context, NewsWebView.class);
intent.putExtra("id", url);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(contentIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1 , builder.build());
}
}
`
You've to read incoming intent OnCreate()
Intent read_int = getIntent();
Log.d("INTENT", read_int.toUri(0));
Then you can break down the incoming Uri request with:
String uri = read_int.getStringExtra("uri"); // your data sent over FCM
And then condition, what activity you want to launch:
if(uri != null){
Intent openWV = new Intent(CurrentActivity.this, MyWebViewActivity.class);
openWV.putExtra("open_url", uri); // optional parameters
CurrentActivity.this.startActivity(openWV);
}
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.