0

I would like to ask if someone can give me an example that uses NotificationCompat.Builder with a foreground service. Can anyone do that please?

public void onStartCommand (Intent intent, int flags, int startId) {
    NotificationCompat.Builder  mNotifyBuilder = new NotificationCompat.Builder ( this) 
       .setContentTitle("somthing");
    mNotificationManager.notify (mNotificationId,mNotifyBuilder.build());

}

When I use this in the foreground it refuses, so how can i use them together?

adelphus
  • 10,116
  • 5
  • 36
  • 46

1 Answers1

0

Quoting the Android docs:

At a minimum, all notifications consist of a base layout, including:

  • The notification's icon. The icon symbolizes the originating app. It may also potentially indicate notification type if the app generates more than one type.
  • A notification title and additional text.
  • A timestamp.

In your code, you are only setting the title. You need use the setSmallIcon(), setLargeIcon(), setContentText() and setWhen() methods of the NotificationCompat.Builder instance before calling build().

adelphus
  • 10,116
  • 5
  • 36
  • 46
  • But i have another question please how can i stop process to restart my service every time i kill app by long press on home button and swipe the app to kill it – khaledsamer Aug 28 '15 at 20:56
  • See if [this question](http://stackoverflow.com/questions/25207383/stop-android-service-when-app-is-closed-with-the-task-manager) can help you. Have a go at it yourself and if you get stuck, post another question for people to help you. – adelphus Aug 28 '15 at 21:06