turtleboy
turtleboy

Reputation: 7572

android push notification opens the browser

I'm following the tutorial below for Google Push Notifications. The tutorial uses WAMP but i've got it to work with a SQL Server. Everything works fine. I can generate a message from the php file on the server and the message is delivered to the registered phones.

The problem i have is the message is a URL to an apk that is the app's upgrade url. When the phone receives the message, the message is in the notification draw and disappears when clicked. What i would like to happen is when the user clicks the notification, the phones browser is opened. this will start downloading the new app.

How can i make android open the browser if the message is a url?

Thanks in advance,

Matt

Tutorial.

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


    };

[EDIT] this is the code that executes when receiving a push. It's in the PushTestActivity class

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)

Upvotes: 0

Views: 2456

Answers (1)

Damien R.
Damien R.

Reputation: 3373

Try something like this, when you click on your notification, get the url and start and intend with Intent.ACTION_VIEW instead of an activity:

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

Upvotes: 1

Related Questions