Remarque: 
 Concernant startForeground (id, notification), si id est 0, il plante, si id est 1, la fonction de notification est désactivée, id est 2 J'ai rencontré le phénomène d'être notifié. Notez que c'était OK si ce n'était pas 0 dans la rue, mais ce n'était pas le cas dans mon code, donc il est mis à 2 dans le code  font>  td>  table>
Notification.Builder(context,channelId) || NotificationCompat.Builder(context,channelId)Le contexte est requis pour utiliser la zone de notification par le système Android et n'existera pas au-delà de la durée de vie de l'objet Builder généré.
** 1. Code de notification avant API25 **
** 2. Code de notification après API26 **
** 3. code supplémentaire du manifeste pour Android9 Pie et versions ultérieures **
** 4. Code qui détermine l'API 25 ou antérieure et l'API 26 ou ultérieure **
1. Code de notification avant API25
- Paramètres de contenu avec le constructeur
--Afficher la notification avec notification
 
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel_id");
   builder.setSmallIcon(R.drawable.notification_icon)
          .setContentTitle("title under 8")
          .setContentText("content under 8")
          .setPriority(NotificationCompat.PRIORITY_DEFAULT);
   NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
   manager.notify(0,builder.build());
}
2. Code de notification API26 ou version ultérieure
- Paramètres de contenu avec le constructeur
 
- Définir l'importance sur le canal
--Générer un canal
--Afficher la notification avec notification
 
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel_id");
   builder.setSmallIcon(R.drawable.notification_icon)
           .setContentTitle("title over 8")
           .setContentText("content over 8");
   int importance = NotificationManager.IMPORTANCE_DEFAULT;
   NotificationChannel channel = new NotificationChannel("channel_id","name",importance);
   NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
   manager.createNotificationChannel(channel);
   manager.notify(0,builder.build());
}
3. code supplémentaire manifeste pour Android9 Pie ou version ultérieure
AandroidManifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
4. Code qui détermine l'API 25 ou version antérieure et l'API 26 ou version ultérieure
if(Build.VERSION.SDK_INT >=26)
MainActivity
String CHANNEL_ID = "channel_id";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createNotificationChannel();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.notification_icon)
           .setContentTitle("title")
           .setContentText("content")
           .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}
private void createNotificationChannel(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"channel_name",importance);
        NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }
}
IntentService et notification
1. Créez une notification avec l'API16
--Créer un projet avec l'API de ciblage de studio Android16
- Le service utilise le service d'intention
 
- Sortie de l'émulateur Android4.1 JellyBean API16
 
** Code de service d'intention sans jugement de niveau API **
** Code de service d'intention avec jugement de niveau API **
Code startService API16 (MainActivity)
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this,MyService.class);
    if(Build.VERSION_SDK_INT >= 26){
       startForegroundService(intent);
    }else{
       startService(intent);
    }
}
Code IntentService sans jugement de niveau API
IntentService
@Override
public int onStartCommand(Intent intent,int flag,int starId){
    Context context = getApplicationContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"id")
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("Title")
            .setPriority(NotificationCompat.PRIORITY_LOW);
    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    manager.notify(1,builder.build());
    function();
    return START_STICKY;
}
public void function(){
//Que veux-tu faire
}
Code IntentService avec niveau API
IntentService
public MyIntentService() {
    super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel");
    builder.setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("sample_title")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .build();
    if(Build.VERSION.SDK_INT >=26){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("channel","name",importance);
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);
    }
    NotificationManagerCompat compatManager =NotificationManagerCompat.from(this);
    startForeground(1,builder.build());
    compatManager.notify(1,builder.build());
    function();
    return START_STICKY;
}
public void function(){
//Que veux-tu faire
}
2. Créez une notification avec l'API29
--Créer un projet avec l'API de ciblage de studio Android29
- Le service utilise le service d'intention
 
- Sortie de l'émulateur Android9.0 Pie API29
 Emulator Même s'il est sorti sur Android 4.1, il a été notifié normalement 
Code MainActivity API29
MainActivity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this,MyIntentService.class);
    if(Build.VERSION.SDK_INT >= 26){
        startForegroundService(intent);
    }else{
        startService(intent);
    }
}
Code IntentService API29
IntentService
protected void onHandleIntent(Intent intent) {
    Notification notification;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
    notification = builder.setSmallIcon(R.drawable.notification_icon)
                          .setContentTitle("title")
                          .setContentText("text")
                          .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                          .build();
    startForeground(2,notification);
    if(Build.VERSION.SDK_INT >= 26){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("id","name",importance);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
    managerCompat.notify(1,notification);
    function();
    return START_STICKY;
}
public void function(){
//Que veux-tu faire
}
Service et notification
AndroidManifest.java
AndroidManifest.java
<manifest ...
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
MainActivity.java
--Pour API26 et versions ultérieures, démarrez le service avec startForegroundService (intent).
- Pour API26 ou moins, démarrez le service avec startService (intent).
 
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Intent intent = new Intent(this,MyService.class);
    Button btn_start = findViewById(R.id.btn_start);
    Button btn_stop  = findViewById(R.id.btn_stop);
    btn_start.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if(Build.VERSION.SDK_INT >=26){
               startForegroundService(intent);
            }else{
               startService(intent);
            }
        }
    });
    btn_stop.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            stopService(intent);
        }
    });
}
MyService.java
MyService.java
@Override 
public int onStartCommand(Intent intent,int flags,int startId){
   //Définir le contenu de la notification
   Notification notification;
   Intent intent_pending = new Intent(this,MainActivity.class);
   PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent_pending,0);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
   notification = builder.setSmallIcon(R.drawable.notification_icon)
       .setContentTitle("Title")
       .setContentText("Text")
       .setContentIntent(pendingIntent)
       .build();
   //API26 et supérieur: startForeground(notificationId,notification)Affichage des notifications avec
   //Générez un canal en définissant l'importance du canal pour chaque notification
   if(Build.VERSION.SDL_INT >= 26){
      int importance = NotificationManager.IMPORTANCE_LOW;
      NotificationChannel channel = new NotificationChannel("id","nama",importance);
      NotificationManager manager = getSystemService(NotificationManager.class);
      manager.createNotificationChannel(channel);
      startForeground(1,notification);
   }else{
      //Inférieur à API26: définir l'importance de chaque composant
      // notify(notificationId,notification)Affichage des notifications avec
      NotificationManagerCompat managerCompat = NotificationCompat.from(this);
      notification = builder.setPriority(NotificationCompat.PRIORITY_LOW).build();
      managerCompat.notify(1,notification);
   }
   // ToDo
   Log.d("msg","Mesage");
   //
   return START_STICKY;
}