Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: notification from BroadcastReceiver

Tags:

I have an alarm manager that starts a broadcast receiver. Here is my broadcast receiver:

public class AlarmBrodcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        showNotification(context);
    }

    private void showNotification(Context context) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MyActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(0)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }  
}

The broadcast starts in time, but there is no notification, only sound. Where is the text? What's wrong? Is it because I'm using API 10 and the support library?

like image 689
Jim Avatar asked Oct 16 '12 23:10

Jim


People also ask

What is BroadcastReceiver Android?

1.1. Definition. 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.

How pass data from BroadcastReceiver to activity in Android?

getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme. Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.

How do I turn off broadcast receiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is notification receiver?

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. Once the event occurs, the system will notify all the registered applications.


1 Answers

Oh, I found the problem. The problem is in .setSmallIcon(0)...When I set some real resource, its ok, notification appear...

like image 125
Jim Avatar answered Oct 19 '22 01:10

Jim