11

Short question:

I'm trying to use the NotificationCompat.Builder class in order to create a notification that will be used for the service, but for some reason, i either don't see the notification, or can't cancel it when the service should be destroyed (or stopping from being in the foreground) .

my code:

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    final String action = intent == null ? null : intent.getAction();
    Log.d("APP", "service action:" + action);
    if (ACTION_ENABLE_STICKING.equals(action)) {
        final NotificationCompat.Builder builder = new Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle("content title");
        builder.setTicker("ticker");
        builder.setContentText("content text");
        final Intent notificationIntent = new Intent(this, FakeActivity.class);
        final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder.setContentIntent(pi);
        final Notification notification = builder.build();
        // notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        // notification.flags |= Notification.FLAG_NO_CLEAR;
        // notification.flags |= Notification.FLAG_ONGOING_EVENT;

        startForeground(NOTIFICATION_ID, notification);
        // mNotificationManager.notify(NOTIFICATION_ID, notification);

    } else if (ACTION_DISABLE_STICKING.equals(action)) {
        stopForeground(true);
        stopSelf();
        // mNotificationManager.cancel(NOTIFICATION_ID);
    }
    return super.onStartCommand(intent, flags, startId);
}

The commented commands are my trials to make it work. none worked for some reason.

I even added a fake activity since it wanted a contentIntent , but it still doesn't work.

Can anyone please help?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

9

I had the exact same problem a while ago, and I found out that for some reason, the notification ID 0 doesn't work well with startForeground(), is it the value of NOTIFICATION_ID in your code?


EDIT: the documentation has now been updated to state that 0 is an invalid ID

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • yes . it is. didn't think of changing it since i usually start with this number for everything... sure it will fix it? – android developer Apr 28 '13 at 19:00
  • I had found the answer [there](http://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification), apparently it is known. It fixed it for me as well as the OP of that post, so it should fix your problem too. Best way to know is to try :) – Joffrey Apr 28 '13 at 22:59
  • And eventually you do! Please accept the answer if you can confirm your problem is solved :) I hope they will fix it soon, because it is really annoying for anyone using `startForeground` for the first time, we almost all use 0 as the ID and spend hours looking for the source of our problem... – Joffrey Apr 28 '13 at 23:15
  • sorry i didn't mean to offend you in any way. i've now tested it and this is correct. using ID value 1 worked just fine. – android developer Apr 29 '13 at 07:36
  • Wasn't offended at all, just saying Android developers should correct it (either the doc or the framework) ^^ I'm glad it works for you now! – Joffrey Apr 29 '13 at 13:41
  • For the record, this is not a bug; the documentation clearly states that this value should not be 0: https://developer.android.com/reference/android/app/Service.html – personne3000 Oct 07 '14 at 11:36
  • @personne3000 Yes, and we are in 2014 now, the documentation has been updated, at last! There used to be nothing about that invalid ID, that's why I said 1.5 year ago that they should update the doc. – Joffrey Oct 07 '14 at 12:07