使用WebView进行Android推送通知?

时间:2012-05-07 04:24:35

标签: android push-notification android-webview

我按照本教程创建了推送通知应用: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");

    // Use the Notification manager to send notification
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // Create a notification using android stat_notify_chat icon. 
    Notification notification = new Notification(android.R.drawable.stat_notify_chat, app_name + ": " + message, 0);

    // Create a pending intent to call the webviewactivity when the notification is clicked
    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; 
    // Set the notification and register the pending intent to it
    notification.setLatestEventInfo(context, app_name, message, pendingIntent); //

    // Trigger the notification
    notificationManager.notify(0, notification);
}

但是,链接到main.xml的HomeActivity只是一个注册和取消注册设备以接收通知的按钮。我在layout:

下创建了另一个文件webviewactivity.xml
 <?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 {

/** Called when the activity is first created. */
@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");

    // TODO Auto-generated method stub
}

  }

出于某种原因,虽然当我点击我的通知时,它会打开主页活动,而不是webview活动。

1 个答案:

答案 0 :(得分:3)

让通知打开您的WebView应用:发送PendingIntent,如here所述。