我正在处理一个通知项目,我的要求是在通知上传递一个url链接,当用户点击通知时,它会导航到一些活动X并在活动X中的Web视图中打开该URL。 / p>
这就是我正在做的......
Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Intent mIntent = new Intent(this, MainActivity.class);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
Bundle bundle = new Bundle();
bundle.putString("test", "test");
notificationIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
打开网址,但不在我的网页浏览中,而是要求选择手机中的浏览器..但我只想在我的网页浏览中打开该链接!
由于
答案 0 :(得分:2)
通知服务
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String intentUri = remoteMessage.getData().get("link");
sendNotification(intentUri , title , body);
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String url, String title, String body) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (url != null) {
intent.putExtra("URL", url);
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = "Default";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
WEBVIEWACTIVITY
Intent intent = this.getIntent();
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("URL"))
{
Url = extras.getString("URL");
}
}
if (Url != null) {
webView.loadUrl(Url);
} else {
webView.loadUrl("https://www.example.com");
}
答案 1 :(得分:0)
无需在通知中传递网址。在通知中将其作为String传递,在接收端将该字符串转换为url。
例如:
Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
notificationIntent.putExtras(bundle);
在receiverEnd上将其转换为
String url = getArguments.getString("url");
Url myUrl = new Url(url);
在webView中打开myUrl。
答案 2 :(得分:0)
您必须使用webview创建新活动。
在您的网页浏览活动中使用此代码
public class WebViewActivity extends Activity {
private WebView webView;
Bundle extras;
String loadurl;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
extras=getIntent().getExtras();
if(extras!=null){
loadurl=extras.getString("url","your default web page");
}
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(loadurl);
}
}