15

I am working on an application that uses a foreground service. for this purpose I am calling startForeground(id,Notification) from inside onStartCommand callback of the service.

I use a notification builder to create my notification but when I pass it to startForeground only the ticker text is displayed as i set it, everything else turns to default, i.e. the Title says " is running while i had it set to " is online"

Also anything that i had set using the setText and setInfo method in the notification.Builder does not show up instead default text like "touch for more information or to stop the application" shows in its place.

here's the relevant code:

Service :

private final int NOTIFICATION_ID=1;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"EDI: Core service Started" , Toast.LENGTH_LONG).show();
        startForeground(NOTIFICATION_ID, CoreServiceNotification.getNotification(this, "EDI is online", "Online","Online and running","EDI just started"));
        return super.onStartCommand(intent, flags, startId);
    }

CoreServiceNotification:

public class CoreServiceNotification {

        public static Notification getNotification(Context context,String title,String text,String info,String tickerText){
            Notification.Builder notificationBuilder= new Notification.Builder(context);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(text);
            notificationBuilder.setContentInfo(info);
            notificationBuilder.setTicker(tickerText);
            notificationBuilder.setLights(0x00ffff00, 1000, 0);
            return notificationBuilder.build();
        }

    }

RESULT: enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Allahjane
  • 1,920
  • 3
  • 24
  • 42
  • I think you forget to Create `NotificationManager notificationManager;` Object then you have to add like `notificationManager.notify("1",notificationBuilder);` – M D Apr 18 '14 at 06:40
  • 1
    No, I am passing the notification object to StartForeground method of the service see the method here [link](http://developer.android.com/reference/android/app/Service.html ) – Allahjane Apr 18 '14 at 06:43
  • then what is your problem? – M D Apr 18 '14 at 06:44
  • please read the question ! the text i set is not what is showed in actual notification – Allahjane Apr 18 '14 at 06:45
  • ohh i did not see sorry! – M D Apr 18 '14 at 06:46
  • I have the same problem, did you ever resolve it? – jeremija May 04 '14 at 14:04
  • 7
    Try calling `notificationBuilder.setSmallIcon(R.drawable.ic_launcher)` before building the notification and calling `startForeground()`, it fixed the problem for me. – jeremija May 04 '14 at 14:13

1 Answers1

36

I think it's necessary to set the smallIcon first when you create the notification.

Notification.Builder notificationBuilder= new Notification.Builder(context);
notificationBuilder.setSmallIcon(R.drawable.yourIcon);
// then set other staff...

Here is another simple example from Cousera Android class, Click here

taffykula
  • 566
  • 7
  • 8