2012-05-07 63 views
4

我在本教程後創建了一個推送通知應用程序:http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html帶有WebView的Android推送通知?

它工作正常,並且當我得到通知時,我可以打開一個網站。但是,我能夠將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只是一個按鈕,註冊和註銷您的設備,用於接收通知。我創建了另一個文件,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活動。

+0

嗨,我是新的android應用程序,我想知道的是一個webview應用程序在用戶的可用性(電子商務網站)方面良好?我有一個電子商務網站,我將它轉換成webview android應用程序,然後將ii能夠發送pushnotification給我的應用程序用戶? 請幫我這個 –

回答

3

讓通知打開您的WebView應用程序:改爲發送PendingIntent,如here所述。

+0

因此,WebView應用程序實際上必須是pushnotifications應用程序中的單獨應用程序(Eclipse中的不同項目文件夾)?如果是這樣,那麼如何將WebView應用程序導入pushnotifications應用程序? – Kevin

+0

不,您可以將WebView放入同一個應用中的活動中。 –

+0

好的。所以我將我的WebView建立在本教程的基礎之上:http://developer.android.com/guide/webapps/webview.html。但是我對如何整合WebView活動有些困惑。我會在pushnotifications中爲WebView製作新的活動嗎?現在pushnotifications的main.xml是一個佈局,只註冊並顯示設備的註冊ID。當我試圖將WebView複製到它中時,出現了一些錯誤(忘記了確切的錯誤,但我可以查看併發布它)。謝謝 – Kevin