我的通知中有一个有效负载,可以发送一个URL,以便当用户点击通知时,它将打开我的Web视图。当应用程序在后台运行时,此功能非常有用。
为了处理前景,我将onMessageReceived
发送URL到我的意图
MyFirebaseMessagingService.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle(remoteMessage.getNotification().getTitle()).setAutoCancel(true).setContentIntent(pendingIntent)
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
if(remoteMessage.getData().size() > 0){
String url = remoteMessage.getData().get("url");
if (url != null) {
intent.putExtra("url", url);
}
}
}
我正努力从堆栈上的许多其他文章中了解到如何在我的mainActivity中接收此意图并如何使用新的URL重新加载Webview。这是我需要整合到onResume
中的东西吗?
似乎在某个时候我必须开始我的意图活动,因为如果在startActivity(intent)
中将putextra
放在onMessageReceived
之后,它将自动设置Webview的网址,而无需用户在这是我不想要的收到通知时必须点击通知。我要在他们单击系统任务栏中的通知后,使Webview网址更改。那我需要在MainActivity.java中startActivity(intent)
吗?
intent.putExtra("url", url);
startActivity(intent);
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
String url = null;
if(isNetworkAvailable()){
if(getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
if (key.equals(("url"))) {
url = getIntent().getExtras().getString(key);
}
}
}
if (url != null) {
webView.loadUrl(url);
} else {
webView.loadUrl("https://www.company.com/this/");
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
webView.evaluateJavascript(
"(function() { return (document.getElementById('user_id_firebase').innerHTML); })();",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String html) {
html = html.replaceAll("[\"]+", "");
StringTokenizer currentString = new StringTokenizer(html, "|");
final String first = currentString.nextToken().trim();
final String second = currentString.nextToken().trim();
// Write a message to the database
database = FirebaseDatabase.getInstance().getReference();
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
if (first.equals("gone")) {
database.child("users").child(second).child("android").child(getMacAddr()).setValue("");
} else if (first.equals("none")) {
database.child("users").child(second).child("android").child(getMacAddr()).setValue("");
} else if (first.equals("active")) {
database.child("users").child(second).child("android").child(getMacAddr()).setValue(newToken);
}
}
});
}
});
}
}
});
} else {
webView.loadUrl("file:///android_asset/nointernet.html");
}
}