vosmith
vosmith

Reputation: 5688

NotificationCompat.Builder not working in Gingerbread

I'm attempting to use the NotificationCompat.Builder class in my app. The following code runs perfectly fine on an ICS phone, but not Gingerbread. From my understanding, doesn't the support library allow access to the Builder from phones as low as API level 4? Am I doing something fundamentally wrong?

public class MainActivity extends Activity implements OnClickListener {
NotificationCompat.Builder builder;
NotificationManager nm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        builder = new NotificationCompat.Builder(this);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        builder.setContentTitle("Test Notification")
        .setContentText("This is just a test notification")
        .setTicker("you have been notified")
        .setSmallIcon(R.drawable.ic_stat_example)
        .setWhen(System.currentTimeMillis());

        findViewById(R.id.btn_notify).setOnClickListener(this);
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0.getId() == R.id.btn_notify){
        nm.notify(1, builder.build());
        }
    }
}

Upvotes: 1

Views: 6795

Answers (1)

Tomonari Ikeda
Tomonari Ikeda

Reputation: 81

You have to supply the Intent which will be triggered when the notification is clicked. Otherwise, the notification will not be displayed on Gingerbread. If you do not want to start the Activity when the notification is clicked, you can just pass an empty Intent as following.

Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

builder.setContentTitle("Test Notification")
    .setContentText("This is just a test notification")
    .setTicker("you have been notified")
    .setSmallIcon(R.drawable.ic_stat_example)
    .setWhen(System.currentTimeMillis())
    .setContentIntent(pendingIntent); // add this

Upvotes: 8

Related Questions