I have an application that is basically web browsing and GCM. I want to achieve the following: if the user is in the application and receives a notification when he clicks on the notification, I want the webview to download the URL specified in the notification.
I am trying to accomplish this using a broadcast receiver, but it does not work.
I dynamically register the receiver in MainActivity:
private void registerNotificationReceiver() { final IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_LOAD_URL_FROM_NOTIFICATION); Log.i(TAG, "registerNotificationReceiver()"); this.receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "notification received"); } }; super.registerReceiver(this.receiver, filter); }
And in the GCM receiver, I use PendingIntent.getBroadast ():
final Intent broadcastIntent = new Intent(MainActivity.ACTION_LOAD_URL_FROM_NOTIFICATION); PendingIntent intent = PendingIntent.getBroadcast(getApplicationContext(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(intent); notification = notificationBuilder.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, notification);
I do not understand why onReceive in the MainActivity class is not called. The message "received notification" is not displayed. Could you help me? Thanks:)
source share