如何从android gcm通知打开src / www / index.html?

时间:2014-12-15 12:41:44

标签: android cordova notifications google-cloud-messaging

请问这个问题的最佳方法是什么?我在我的Android应用程序中设置GCM通知并且它工作正常,但是,我需要在状态栏上的通知被点击打开位于src / www /中的index.html。

PLS。认真等待你的预期帮助。感谢。

1 个答案:

答案 0 :(得分:1)

您可以创建一个可以使用webView访问src / www / index.html的活动,如布局和

只需编写以下代码:

在资产中创建www文件夹

        String mFileName = "file:///android_asset/www/index.html";
        WebView mWebView = (WebView) findViewById(R.id.web_view);

        WebSettings settings = mWebView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        mWebView.loadUrl(mFileName);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().length() == 0) {
                    return false;
                }

                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                    return true;
                } catch (Exception e) {
                    CustomizeCrouton.showCrouton(R.string.no_browser, AboutUsActivity.this);
                    return false;
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mSpinnerDlg.dismiss();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                mSpinnerDlg.dismiss();
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });

然后在通知单击

上通过PendingIntent调用此活动
 private static void generateNotification(Context context, String message) {
            int icon = R.drawable.ic_launcher;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);

            String title = context.getString(R.string.app_name);

            Intent notificationIntent = new Intent(context, RawFolderContentActivity.class);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;

            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);      

        }