Max Mumford
Max Mumford

Reputation: 2642

Android show notification from BroadcastReceiver

I have a class which extends BroadcastReceiver that gets called whenever new Wifi scan results are available (the receiver is registered in the manifest with the Scan_Results broadcast as the intent-filter).

From this class, I want to be able to show a notification to the user. Currently, I pass the context that is received as a parameter in the onReceive method of my broadcast intent class to a "show notification" method of another class.

When it gets to the line:

myNotificationManager.notify(notificationId, notification);

it fails with the following exception:

java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)

Any idea why this is happening? All I can think of is because the context that I am getting from the onReceive parameter is not ... for lack of a better phrase, "right for the job"...

Any ideas? Thanks, Max.

Upvotes: 7

Views: 13510

Answers (6)

aagam94
aagam94

Reputation: 613

For those who are using NotificationCompat, the following code will work:

NotificationCompat.Builder n = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent i = new Intent(this,MainActivity.class);
    PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
    n.setContentIntent(ac);
    nm.notify(12222, n.build());

Upvotes: 0

Jovin
Jovin

Reputation: 11

Use this code along with your notification

Intent intent = new Intent(this, MusicDroid.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title", 
    "This is the text", activity);
notification.number += 1;

nm.notify(NOTIFY_ID, notification);

Upvotes: 1

Chaitanya K
Chaitanya K

Reputation: 1847

ContentView is the view which is required when the notification is clicked. The code below works fine and setLatestEventInfo() is required method.

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Hello from service", System.currentTimeMillis());
    Intent intent = new Intent(this, MainActivity.class);
    notification.setLatestEventInfo(this, "contentTitle", "contentText",
            PendingIntent.getActivity(this, 1, intent, 0));
    manager.notify(111, notification);

Upvotes: 13

Mike Reiche
Mike Reiche

Reputation: 460

You have to call Notification.setLatestEventInfo().

Upvotes: 2

Max Mumford
Max Mumford

Reputation: 2642

Not sure exactly why it wasnt working before but here is the code I got it working with:

Declare the following outside of any method:

int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

Then in the onReceive method call the following:

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);

Then declare the following method:

private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
        // This is who should be launched if the user selects our notification.
        Intent contentIntent = new Intent();

        // choose the ticker text
        String tickerText = "ticket text";

        Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());

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

        n.setLatestEventInfo(context, "1", "2", appIntent);

        mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
    }

Upvotes: 2

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

From what I can tell, when you're creating you notification to pass to the Notification manager, you're not giving it a content view to display. Review the line where you actually create the notification to see if you're actually giving the notification a view to display.

Upvotes: 0

Related Questions