android推送通知打开浏览器

时间:2013-04-23 11:46:38

标签: android push-notification

我正在按照下面的Google推送通知教程进行操作。本教程使用WAMP,但我已经使用它与SQL Server一起工作。一切正常。我可以从服务器上的php文件生成一条消息,并将消息传递给已注册的手机。

我遇到的问题是消息是一个apk的URL,是应用程序的升级网址。当手机收到消息时,消息将显示在通知中,并在单击时消失。我想要发生的是当用户点击通知时,手机浏览器被打开。这将开始下载新的应用程序。

如果消息是网址,我如何让android打开浏览器?

提前致谢,

马特

教程。

Tutorial.

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    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, PushTestActivity.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);     

    }

 /**
     * Receiving push messages
     * */
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());


            /**
             * Take appropriate action on this message
             * depending upon your app requirement
             * For now i am just displaying it on the screen
             * */

            if(newMessage != null){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newMessage));
            startActivity(browserIntent);
            }else{
                Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

            }

            // Showing received message
            //lblMessage.append(newMessage + "\n");
            //Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

            // Releasing wake lock
            WakeLocker.release();
        }


    };

[编辑] 这是接收推送时执行的代码。它位于PushTestActivity类

04-23 13:12:52.630: E/AndroidRuntime(16231): FATAL EXCEPTION: main
04-23 13:12:52.630: E/AndroidRuntime(16231): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.carefreegroup.pushtest.DISPLAY_MESSAGE flg=0x10 (has extras) } in com.carefreegroup.pushtest.PushTestActivity$1@412b29b0
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:846)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.os.Handler.handleCallback(Handler.java:615)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.os.Looper.loop(Looper.java:155)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.ActivityThread.main(ActivityThread.java:5485)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at java.lang.reflect.Method.invoke(Method.java:511)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at dalvik.system.NativeStart.main(Native Method)
04-23 13:12:52.630: E/AndroidRuntime(16231): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=null }
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1671)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1542)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Activity.startActivityForResult(Activity.java:3409)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Activity.startActivityForResult(Activity.java:3370)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Activity.startActivity(Activity.java:3580)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.Activity.startActivity(Activity.java:3548)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at com.carefreegroup.pushtest.PushTestActivity$1.onReceive(PushTestActivity.java:142)
04-23 13:12:52.630: E/AndroidRuntime(16231):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:832)

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,当您点击通知时,获取网址并开始并打算使用Intent.ACTION_VIEW而不是活动:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(yourUrlInString));
startActivity(browserIntent);