Android 推送通知未使用 webview 打开我的活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10476616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:53:37  来源:igfitidea点击:

Android Push Notification not opening my activity with webview

androidpush-notificationandroid-webview

提问by Kevin

I created a push notifications app following this tutorial: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

我按照本教程创建了一个推送通知应用程序:http: //www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

It works and when I get the notification, I can open a website. However, am I able to combine the webview layout with this push notifications app? I feel like it has to be possible because the email notifications work in that way.

它有效,当我收到通知时,我可以打开一个网站。但是,我可以将 webview 布局与此推送通知应用程序结合使用吗?我觉得它必须是可能的,因为电子邮件通知以这种方式工作。

Here is my code which handles the notification I receive:

这是我处理收到的通知的代码:

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

However, the HomeActivity which is linked to the main.xml is just a button to register and unregister your device for receiving the notifications. I created another file, webviewactivity.xml under layout:

但是,链接到 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"
 />

and I created the following activity, webviewactivity

我创建了以下活动 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");
    }
}

For some reason though when I click my notification it opens the home activity, not the webview activity.

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

采纳答案by Yusuf X

Make the notification open your WebView app instead: send a PendingIntent, as described here.

请通知打开你的WebView应用程序,而不是:发送的PendingIntent,描述在这里