Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 228
Max Mumford Avatar asked Oct 23 '11 20:10

Max Mumford


People also ask

How to create Android notification with BroadcastReceiver?

2) Implement the onReceive() method: In order for the notification to be sent, an onReceive() method has to be implemented. Whenever the event for which the receiver is registered occurs, onReceive() is called. For instance, in case of battery low notification, the receiver is registered to Intent.

What is the role of the onReceive() method in the BroadcastReceiver?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

How a notification manager and broadcast receiver works under Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


2 Answers

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);
like image 181
Chaitanya K Avatar answered Sep 18 '22 23:09

Chaitanya K


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);
    }
like image 43
Max Mumford Avatar answered Sep 20 '22 23:09

Max Mumford