如何从通知中获取 link 并通过 firebase 在 mainactivity webview 中打开?
how to get link from notification and open in mainactivity webview by firebase?
我在做什么: 通过这些代码,我收到来自 Firebase 的通知和打开 mainactivity webview 默认值的 onclick 通知 url。
我想做什么:我想在我将通过 Firebase 发送的 mainactivity webview 中打开 url。
我不知道如何实现这个(从 Firebase 通知接收 url 并在 mainactivity webview 中打开)
notification.java
public class FirebaseMessageReciver extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//handle when receive notification via data event
if (remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
}
//handle when receive notification
if(remoteMessage.getNotification()!=null){
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
}
private RemoteViews getCustomDesign(String title,String message) {
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.title,title);
remoteViews.setTextViewText(R.id.message,message);
remoteViews.setImageViewResource(R.id.icon,R.mipmap.ic_launcher_round);
return remoteViews;
}
public void showNotification(String title,String message,String link){
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(uri)
.setAutoCancel(true)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
builder=builder.setContent(getCustomDesign(title, message));
}
else{
builder= builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round);
}
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(uri, null);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0,builder.build());
}
}
mainactivity.java
public class MainActivity extends AppCompatActivity {
String webAdress = "https://google.com";
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setJavaScriptEnabled(true); //enable javascript
webView.loadUrl(webAdress);
}
private class HelpClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
你可以使用intent.putExtra
像这样在您的代码中添加它。
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
intent.putExtra("link", link); // add this line for send url
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
并在 MainActivity 中添加 getIntent
Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
String urlFromFirebase = getIntent().getStringExtra("link"); //get link from firebase
// for exception
if (urlFromFirebase.equals("")) {
urlFromFirebase = "https://google.com";
}
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setJavaScriptEnabled(true); //enable javascript
webView.loadUrl(urlFromFirebase ); //load
然后您可以从 firebase 加载 url。
[编辑]
我创立了
showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
此代码在您编辑之前,所以如果您想编辑。试试这个代码。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//handle when receive notification via data event
if (remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("body"));
}
//handle when receive notification
if(remoteMessage.getNotification()!=null){
showNotification(remoteMessage.getData().get("body"));
}
}
private void sendNotification(String messageBody) {
try {
JSONObject jsonBody = new JSONObject(messageBody);
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
// you can edit this part as you want or follow data.
String title = jsonBody.getString("title");
String message = jsonBody.getString("message");
String link = jsonBody.getJSONObject("data").getString("link");
intent.putExtra("link", link);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(uri)
.setAutoCancel(true)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
builder=builder.setContent(getCustomDesign(title, message));
}
else{
builder= builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round);
}
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(uri, null);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0,builder.build());
} catch (JSONException e) {
e.printStackTrace();
}
}
在我的例子中,我创建了 JSONObject,并获取了 link 表单数据。试试这个。
我在做什么: 通过这些代码,我收到来自 Firebase 的通知和打开 mainactivity webview 默认值的 onclick 通知 url。
我想做什么:我想在我将通过 Firebase 发送的 mainactivity webview 中打开 url。
我不知道如何实现这个(从 Firebase 通知接收 url 并在 mainactivity webview 中打开)
notification.java
public class FirebaseMessageReciver extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//handle when receive notification via data event
if (remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
}
//handle when receive notification
if(remoteMessage.getNotification()!=null){
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
}
private RemoteViews getCustomDesign(String title,String message) {
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.title,title);
remoteViews.setTextViewText(R.id.message,message);
remoteViews.setImageViewResource(R.id.icon,R.mipmap.ic_launcher_round);
return remoteViews;
}
public void showNotification(String title,String message,String link){
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(uri)
.setAutoCancel(true)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
builder=builder.setContent(getCustomDesign(title, message));
}
else{
builder= builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round);
}
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(uri, null);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0,builder.build());
}
}
mainactivity.java
public class MainActivity extends AppCompatActivity {
String webAdress = "https://google.com";
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setJavaScriptEnabled(true); //enable javascript
webView.loadUrl(webAdress);
}
private class HelpClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
你可以使用intent.putExtra
像这样在您的代码中添加它。
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
intent.putExtra("link", link); // add this line for send url
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
并在 MainActivity 中添加 getIntent
Log.d("Token", ""+ FirebaseInstanceId.getInstance().getToken());
FirebaseMessaging.getInstance().subscribeToTopic("allDevices");
String urlFromFirebase = getIntent().getStringExtra("link"); //get link from firebase
// for exception
if (urlFromFirebase.equals("")) {
urlFromFirebase = "https://google.com";
}
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setJavaScriptEnabled(true); //enable javascript
webView.loadUrl(urlFromFirebase ); //load
然后您可以从 firebase 加载 url。
[编辑] 我创立了
showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("link"));
此代码在您编辑之前,所以如果您想编辑。试试这个代码。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//handle when receive notification via data event
if (remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("body"));
}
//handle when receive notification
if(remoteMessage.getNotification()!=null){
showNotification(remoteMessage.getData().get("body"));
}
}
private void sendNotification(String messageBody) {
try {
JSONObject jsonBody = new JSONObject(messageBody);
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "web_app_channel";
// you can edit this part as you want or follow data.
String title = jsonBody.getString("title");
String message = jsonBody.getString("message");
String link = jsonBody.getJSONObject("data").getString("link");
intent.putExtra("link", link);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setSound(uri)
.setAutoCancel(true)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
builder=builder.setContent(getCustomDesign(title, message));
}
else{
builder= builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round);
}
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(channel_id,"web_app",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(uri, null);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0,builder.build());
} catch (JSONException e) {
e.printStackTrace();
}
}
在我的例子中,我创建了 JSONObject,并获取了 link 表单数据。试试这个。