Android Push Notification not opening my activity with webview

44,749

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

Share:
44,749
Kevin
Author by

Kevin

Updated on July 09, 2022

Comments

  • Kevin
    Kevin almost 2 years

    I created a push notifications app following this tutorial: 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.

    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:

     <?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

    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.