我试图在点击通知时打开网页:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.setData(Uri.parse("htp://www.google.com"));
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
我毫无问题地收到了通知,但是当我单击它时,它只是将我带到应用程序。
最佳答案
将您的 notifyIntent
更改为:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
那么你的代码将是这样的:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
关于java - Android点击通知打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316397/