I am using a webview application and want to load the url which received in notification.
For that I am using putExtra
to receive the url in main activity but I am getting null.
Here is my code FcmMessagingService code -
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("fcm_url", "here url will be there");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Message");
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Message", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
This is my MainActivity onCreate code -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
final String fcm_message = intent.getStringExtra("fcm_url");
if(fcm_message!=null){
Log.d("Fcm_url",fcm_message);
}
else{
Log.d("Fcm_url","This is null");
}
...
When I open app directly logcat prints This is null
which is fine but when I send a notification and open the app by clicking notification, it again print This is null
, what I am doing wrong ?
Update
When I add Log.d..
inside my onMessageRecieved
there is no execution of it see
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("on_message_recieved","fcm message recieved");
....
but it is not printing after receiving notification ???
In my ManiFest I had added this -
<service
android:name=".FcmMessagingService"
android:permission="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
It seems that it is not executing my custom FcmMessagingService
but why ??