2012-05-07 67 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