0

Using Firebase I am trying to send notification in a 'key' with a 'value' URL link. I got the notification every time, but the problem is which URL link I send in my value that wouldn't open in my webview.

Here I've attached my webview activity

public class WebViewActivity extends AppCompatActivity {

private WebView notiWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    notiWebView = (WebView) findViewById(R.id.NotiWebView);

    if(getIntent().getExtras() != null){
        for(String key: getIntent().getExtras().keySet()){
            if(key.equals(("url"))){
                notiWebView.loadUrl(key);
            }
        }
    }

}

here is my FirebaseInstanceIdService

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh(){
    String token = FirebaseInstanceId.getInstance().getToken();
    Log.d("TOKEN",token);
}

and here is FirebaseInstanceMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this,WebViewActivity.class);
    intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("My 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());

}

Any help will be appreciated. Thanks in advance!

AL.
  • 36,815
  • 10
  • 142
  • 281
Al Walid Ashik
  • 1,545
  • 20
  • 32

1 Answers1

0

after adding LocalBroadcastManager in my code it work fine.i am posting solution if it help someone future

here is my onMessageReceived method in FirebaseMessagingService class

public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getData().size()>0){
        String url = remoteMessage.getData().get("url");
        Intent intent = new Intent("com.FCM-MESSAGE");

        intent.putExtra("url",url);

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.sendBroadcast(intent);
    }

}

and added this below method in MainActivity.class

LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.bellecarib_FCM-MESSAGE"));

and in onCreate method added this below line code and it work fine for me now.

  if(getIntent().getExtras() != null){
        for(String key: getIntent().getExtras().keySet()){
            if(key.equals(("url"))){
                mwebView.loadUrl(getIntent().getExtras().getString(key));
                Toast.makeText(getApplicationContext(),"Opening The Notification Page",Toast.LENGTH_LONG).show();
            }

        }

    }
Al Walid Ashik
  • 1,545
  • 20
  • 32