Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send broadcast on notification click

I have an application which is basically a webview and GCM notifications. I want to achieve the following thing: If the user is in the app and receives a notification, when he clicks the notification I want the webview to load the url provided in the notification.

I'm trying to accomplish this by using broadcast receiver but it doesn't work.

I dynamically register the receiver in the 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 Listener I'm using 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 don't understand why onReceive in the MainActivity class is not called. The message "notification received" is not displayed. Can you help me? Thank you :)

like image 800
dephinera Avatar asked Aug 11 '15 09:08

dephinera


People also ask

How do I send broadcast notifications on Android?

getBroadcast(getApplicationContext(), 0, broadcastIntent, PendingIntent. FLAG_UPDATE_CURRENT); notificationBuilder. setContentIntent(intent); notification = notificationBuilder. build(); notification.

How notification and broadcast is used in Android Studio?

A broadcast receiver is a dormant component of the Android system. Only an Intent (for which it is registered) can bring it into action. The Broadcast Receiver's job is to pass a notification to the user, in case a specific event occurs. Using a Broadcast Receiver, applications can register for a particular event.


1 Answers

I cannot find right now the reason but there's a security reason. Latest Android versions won't allow you to trigger a listener from "kind-of" remote process without it being explicit.

The intent you broadcast MUST be explicit for it to work. Explicit means that you have to explicitly call the component that will handle the intent (the receiver). So this receiver must be declared in its own class and in the manifest as a <receiver>.

Follow this guy's example in the section Explicit Broadcast Intents http://codetheory.in/android-broadcast-receivers/ and thy will be done.

like image 123
eduyayo Avatar answered Oct 09 '22 02:10

eduyayo