2017-02-14 22 views
0

使用Firebase我试图通过'值'URL链接在'钥匙'中发送通知。我每次都收到通知,但问题是我的价值中发送的URL链接不会在我的webview中打开。使用Firebase发送带有在我的WebView中打开的链接的通知

在这里,我附上我的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()); 

} 

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

+0

这可能会帮助你-http://stackoverflow.com/a/42211049/6291914 –

+0

感谢这个@AL –

回答

0

在我的代码加入LocalBroadcastManager后,工作fine.i我张贴的解决方案,如果它帮助别人的未来

这里是我在FirebaseMessagingService类

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

} 

onMessageReceived方法和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(); 
      } 

     } 

    }