Tout ce que @sky-kelsey a décrit est bon, Juste des ajouts mineurs :
Vous ne devez pas enregistrer le même canal à chaque fois s'il a déjà été enregistré, donc J'ai la méthode de la classe Utils qui crée un canal pour moi :
public static final String NOTIFICATION_CHANNEL_ID_LOCATION = "notification_channel_location";
public static void registerLocationNotifChnnl(Context context) {
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager mngr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (mngr.getNotificationChannel(NOTIFICATION_CHANNEL_ID_LOCATION) != null) {
return;
}
//
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_LOCATION,
context.getString(R.string.notification_chnnl_location),
NotificationManager.IMPORTANCE_LOW);
// Configure the notification channel.
channel.setDescription(context.getString(R.string.notification_chnnl_location_descr));
channel.enableLights(false);
channel.enableVibration(false);
mngr.createNotificationChannel(channel);
}
}
strings.xml :
<string name="notification_chnnl_location">Location polling</string>
<string name="notification_chnnl_location_descr">You will see notifications on this channel ONLY during location polling</string>
Et j'appelle la méthode chaque fois que je vais afficher une notification de ce type :
...
NotificationUtil.registerLocationNotifChnnl(this);
return new NotificationCompat.Builder(this, NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION)
.addAction(R.mipmap.ic_launcher, getString(R.string.open_app),
activityPendingIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.remove_location_updates),
servicePendingIntent)
.setContentText(text)
...
Un autre problème typique - le son par défaut du canal - décrit ici : https://stackoverflow.com/a/45920861/2133585
1 votes
Cette erreur n'a rien à voir avec la raison pour laquelle votre notification ne s'affiche pas. Veuillez inclure votre adresse complète
NotificationCompat
que vous utilisez et votretargetSdkVersion
.0 votes
J'avais initialement prévu de répondre à ma propre question, l'erreur citée étant ce que les autres rechercheraient. Maintenant je réalise que l'erreur n'a rien à voir avec le fait qu'elle ne s'est pas affichée. C'est parce que je n'avais pas de NotificationChannel. Android devrait enregistrer les cas où il ne peut pas afficher une notification, ou lancer une exception ou autre chose.
0 votes
Poste connexe - NotificationCompat.Builder n'accepte pas le 2ème argument