Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top