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

标签 android cordova notifications google-cloud-messaging

请问解决这个问题的最佳方法是什么?我在我的 android 应用程序中设置了 GCM 通知,它工作正常,但是,当单击状态栏上的通知以打开位于 src/www/中的 index.html 时,我需要它。

请。认真等待您的预期帮助。谢谢。

最佳答案

您可以创建一个 Activity ,该 Activity 可以在布局中使用 webView 访问您的 src/www/index.html

只需编写以下代码:

在assets中制作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 调用这个 Activity

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

        }

关于android - 如何从 android gcm 通知打开 src/www/index.html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484364/

相关文章:

android - 如何使用 SensorManager.getOrientation 进行倾斜控制,例如 "My Paper Plane"?

android - 使用 Gradle 从 IDEA 迁移到 Android Studio

android - Cordova - 在 Android 上安装 AdMob 插件失败 (Mac)

php - Laravel + Ratchet : Pushing notifications to specific clients

android - Android 模拟器 4.2.2 或更高版本上的 Google Play 服务。 "Google Maps API demos won' t 运行,除非你更新谷歌播放服务”

java - Android文件系统中的内部存储实际上存储在哪里?

android - Phonegap/ionic 应用启动画面未显示

android - 在设备上运行 phonegap - 未找到设备

java - 如何将通知图标设置为大

ios 火灾前本地通知检查