安卓推送通知无法打开我的带有 Webview 的活动页面

12

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

不知何故,当我点击通知时,它打开的是主页活动而不是网页视图活动。


1
嗨,我是一个新手想要开发Android应用程序,想知道使用Webview应用程序是否对用户友好(针对电子商务网站)?我有一个电子商务网站,我想把它转换成Webview Android应用程序,那么我能给我的应用程序用户发送推送通知吗?请帮帮我。 - Nikhil Agrawal
@NikhilAgrawal,WebView 真的太糟糕了。有很多漏洞和缺失的功能。我甚至无法打开PDF文件。 - Black
这个问题的名称有点误导,我以为你是指在Android上的Chrome推送通知。 - Chagai Friedlander
1个回答

4

让通知打开你的WebView应用程序:发送一个PendingIntent,如此处所述。


@Kev:你能否描述一下在webview中实现推送通知的步骤?我一直在尝试,但还没有成功。如果你能帮忙就太好了,期待你的回复。先谢谢了。 - Chirag Nagpal
1
@chiragNandwani 嘿,小伙子,老实说我真的不太记得了,因为这是近两年前的事情了。我很肯定我有我的旧代码,我可以看一下。但我需要刷新一下自己的记忆。我不确定在stackoverflow上是否允许这样做,但如果你留下你的电子邮件地址,我会在一两天后给你发邮件。否则,这是我用过的教程。http://www.vogella.com/tutorials/AndroidCloudToDeviceMessaging/article.html - Kevin
@Kev:好的,那么这是我的电子邮件地址:“er.chiragnagpal@gmail.com”。等待您的回复,希望您能在这方面帮助我。提前致谢。 - Chirag Nagpal
@YusufX,只通过电子邮件与我们分享信息而不是在stackoverflow上与我们所有人分享,这是不公平的。 - Black
@Black 你可以向Kevin提出这个问题。 - Yusuf X
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接