发送带有链接的通知,使用Firebase在My WebView中打开

时间:2017-02-14 19:40:12

标签: android firebase webview firebase-cloud-messaging

使用Firebase我正在尝试使用'密钥发送通知。具有“价值”'网址链接。我每次都收到通知,但问题是我在我的网页浏览中打开了哪个URL链接。

我已经附上 webview活动

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);
            }
        }
    }

}

这是我的 FirebaseInstanceIdService

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

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

这里是 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());

}

任何帮助将不胜感激。提前致谢!

1 个答案:

答案 0 :(得分:0)

在我的代码中添加LocalBroadcastManager之后,它工作得很好。我正在发布解决方案,如果它有助于未来的人

这是我在FirebaseMessagingService类中的onMessageReceived方法

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);
    }

}

并在MainActivity.class

中添加了以下方法
LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.bellecarib_FCM-MESSAGE"));

并在onCreate方法中添加了以下行代码,它现在对我来说很好。

  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();
            }

        }

    }