【问题标题】:Android Push Notification not opening my activity with webviewAndroid 推送通知未使用 webview 打开我的活动
【发布时间】:2012-05-07 04:24:34
【问题描述】:

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

 <?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");
    }
}

由于某种原因,当我单击通知时,它会打开主页活动,而不是 webview 活动。

【问题讨论】:

  • 嗨,我是安卓应用的新手,我想知道一个 webview 应用在用户可用性方面是否良好(对于电子商务网站)?我有一个电子商务网站,我会将其转换为 webview android 应用程序,然后我可以向我的应用程序用户发送推送通知吗?请帮我解决这个问题
  • @NikhilAgrawal,webview 太可怕了。有很多错误和缺失的功能。我什至无法打开 PDF 文件。
  • 问题的名称有点误导,我以为你的意思是 Android 上的 Chrome 推送通知

标签: android push-notification android-webview


【解决方案1】:

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

【讨论】:

  • 所以 WebView 应用程序实际上必须是与 pushnotifications 应用程序不同的应用程序(Eclipse 中的不同项目文件夹)?如果是,如何将 WebView 应用导入 pushnotifications 应用?
  • 不,你可以把WebView放在同一个应用的Activity中。
  • 好的。所以我的 WebView 基于本教程:developer.android.com/guide/webapps/webview.html。但我对如何集成 WebView 活动有点困惑。我会在推送通知中为 WebView 创建一个新活动吗?现在 pushnotifications 的 main.xml 是一个仅注册并显示设备注册 ID 的布局。当我尝试将 WebView 复制到其中时,出现了一些错误(忘记了确切的错误,但我可以查找并发布它)。谢谢
  • 通知只有一个视图,但会启动单独的 WebView 活动。
  • @chiragNandwani 嘿,老实说,我真的不记得了,因为那是差不多两年前的事了。我相当肯定我有我的旧代码,我可以看看。我必须刷新自己。我不确定你是否被允许使用 stackoverflow,但如果你留下你的电子邮件,我可以在我刷新自己后的一两天内给你发电子邮件。 Oterwise 这是我使用的教程。 vogella.com/tutorials/AndroidCloudToDeviceMessaging/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多