我按照这个教程创建了一个推送通知应用:http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
它可以正常工作,当我收到通知时,我可以打开一个网站。但是,我能否将webview布局与此推送通知应用程序结合使用?我感觉这是可能的,因为电子邮件通知就是这样工作的。
这里是我处理接收到的通知的代码:
private void handleData(Context context, Intent intent) {
String app_name = (String) context.getText(R.string.app_name);
String message = intent.getStringExtra("message");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.stat_notify_chat, app_name + ": " + message, 0);
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, new Intent(context, webviewactivity.class), PendingIntent.FLAG_UPDATE_CURRENT); //
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, app_name, message, pendingIntent); //
notificationManager.notify(0, notification);
}
然而,与main.xml关联的HomeActivity仅是一个用于注册和注销接收通知设备的按钮。我创建了另一个文件webviewactivity.xml并放置在layout目录下:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
我创建了以下活动,webviewactivity。
public class BHWebView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bhwebview_activity);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.badgerherald.com");
}
}
不知何故,当我点击通知时,它打开的是主页活动而不是网页视图活动。