12

Is it possible to have a notification start a broadcast receiver?

I tried this code but it doesnt work.

Notification is created but when I click on it nothing happens.

NOTE: When I change the notificationIntent to point from MyBroadcastReceiver.class to an activity (like MainActivity.class) it works fine.

Notification creation:

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(
        Context.NOTIFICATION_SERVICE);

    int notificationIconId = XXXXXX
    Notification notification = new Notification(
        notificationIconId,
        XXXXXX,
        System.currentTimeMillis()
    );

    CharSequence contentTitle = XXXXXXX
    CharSequence contentText = XXXXXX

    Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notificationManager.notify(1,notification);

Here is the BroadcastReceiver

public static class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   /*
          */

 }
}

Inside AndroidManifest.xml

<receiver android:name=".MyBroadcastReceiver" />
Abhishek
  • 1,749
  • 9
  • 27
  • 39

1 Answers1

36

From your code...

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

When creating a PendingIntent targetting a BroadcastReceiver, you have to use getBroadcast(...) and not getActivity(...).

See PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

Also, don't create your Intent like this...

Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);

That is an explicit Intent which targets a specific class (used for starting a specific Activity class usually).

Instead create a 'broadcast' Intent with an 'action' such as...

Intent notificationIntent = new Intent(MyApp.ACTION_DO_SOMETHING);

You'll also need to specify a <intent-filter> section for the <receiver android:name=".MyBroadcastReceiver" /> section of your manifest.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1
    @Abhishek : No problem. First time I worked with notifications, I fell into the same trap with using `getActivity` for a `PendingIntent` when I actually wanted to start a `Service` (which uses `getService`). A lesson well learnt. :) – Squonk Jun 07 '12 at 23:36
  • 3
    @Abhishek you forgot to mention setClass for the notificationIntent `notificationIntent.setClass(context, MyBroadcastReceiver.class);` – AbdullahDiaa Feb 21 '13 at 07:15
  • 5
    Please note that the second part of this answer is not really correct. For manifest-registered Receivers, an explicit `Intent` is perfectly fine, and certainly preferred. Indeed, as of API level 26, for apps targeting that version and above, implicit `Intent`s no longer work for Receivers registered in the manifest, [except for a few specific system broadcasts](https://developer.android.com/guide/components/broadcast-exceptions). You can still set an action on the `Intent`, to discern different broadcasts within the Receiver, but you will not need the `` on the ``. – Mike M. Jan 30 '19 at 02:25