Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Push Notification not opening my activity with webview

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.

like image 581
Kevin Avatar asked May 07 '12 04:05

Kevin


People also ask

How do I use push notifications on web application?

Send push messages Normally, this would require sending a subscription from a web page to a backend. The backend would then trigger a push message by making an API call to the endpoint in the subscription. Under Text to Send, add any string you want to send with the push message. Click the Send push message button.

Is Android WebView deprecated?

Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.

What is WebView activity?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.


1 Answers

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

like image 172
Yusuf X Avatar answered Nov 15 '22 19:11

Yusuf X