Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Intents and Broadcast Receivers Local Notifications Creating a Local Notification

Intents and broadcast recievers

Next we'll need a NotificationManager variable to send our local notification. Set it using the system service Context.NOTIFICATION_SERVICE.

DistressCallReceiver.java
public class DistressCallReceiver extends BroadcastReceiver {
  private NotificationManager mNotificationManager;
  private static final int NOTIFICATION_ID = 22;

    @Override
    public void onReceive(Context context, Intent intent) {
        // Add your code here!
       Notification.Builder notif = new Notification.Builder(context);
        notif.setSmallIcon(R.drawable.ic_distress);
        notif.setContentTitle("Incoming Distress Signal");
        notif.setContentText("A distress call has been received from somewhere in space and time.");
        Notification notification = notif.build();
        mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  
        mNotificationManager.notify(NOTIFICATION_ID, notif.build());

    }
}

2 Answers

Joshua Douce
Joshua Douce
13,120 Points

Hi Patrick, the line mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); needs to be mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

This had me stumped for a long time!

Hi Patrick,

You want to call the method getSystemService on the context that is passed into the onReceive method. Use dot notation.

I'll check to see if the rest of the code you added is essential or if this can be trimmed a little.

Steve.

Hi Patrick,

You can do without creating the member variable; just create a local one within the onReceive method's scope. Same with the ID - there's no need for the constant but neither of those things prevent your code from passing the challenge.

Your key error is not using the context (lowecase) to call the getSystemService() method on.

Let me know how you get on.

Steve.