Notification ne s’affiche pas à Oreo

Normal Notification Builder n’affiche pas les notifications sur Android O

Comment pourrais-je afficher une notification sur Android 8 Oreo?

Y at-il un morceau de code manquant pour afficher une notification sur Android O

Dans Android O, il est indispensable d’utiliser un canal avec votre Notification Builder

ci-dessous est un exemple de code:

// Sets an ID for the notification, so it can be updated. int notifyID = 1; Ssortingng CHANNEL_ID = "my_channel_01";// The id of the channel. CharSequence name = getSsortingng(R.ssortingng.channel_name);// The user-visible name of the channel. int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); // Create a notification and set the notification channel. Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.drawable.ic_notify_status) .setChannelId(CHANNEL_ID) .build(); 

Ou avec la compatibilité de manipulation par:

 NotificationCompat notification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setChannelId(CHANNEL_ID).build(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.createNotificationChannel(mChannel); // Issue the notification. mNotificationManager.notify(notifyID , notification); 

ou si vous voulez une solution simple, utilisez le code suivant:

 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mNotificationManager.createNotificationChannel(mChannel); } 

Mises à jour: Référence NotificationCompat.Builder

 NotificationCompat.Builder(Context context) 

Ce constructeur est devenu obsolète au niveau de l’API 26.0.0, vous devez donc utiliser

 Builder(Context context, Ssortingng channelId) 

donc pas besoin de setChannelId avec le nouveau constructeur.

Et vous devriez utiliser la dernière bibliothèque AppCompat actuellement 26.0.2

 comstack "com.android.support:appcompat-v7:26.0.+" 

Source sur Android Developers Channel sur Youtube

En outre, vous pouvez consulter les documents officiels Android

En plus de cette réponse , vous devez créer le canal de notification avant de pouvoir l’utiliser.

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { /* Create or update. */ NotificationChannel channel = new NotificationChannel("my_channel_01", "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT); mNotificationManager.createNotificationChannel(channel); } 

Vous devez également utiliser des canaux uniquement si votre targetSdkVersion est 26 ou supérieur.

Si vous utilisez NotificationCompat.Builder, vous devez également mettre à jour la version bêta de la bibliothèque de support: https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0- beta2 (pour pouvoir appeler setChannelId sur le constructeur de compat).

Faites attention car cette mise à jour de la bibliothèque augmente minSdkLevel à 14.

Ici, je poste une fonction de solution rapide avec la manipulation intentionnelle

 public void showNotification(Context context, Ssortingng title, Ssortingng body, Intent intent) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); int notificationId = 1; Ssortingng channelId = "channel-01"; Ssortingng channelName = "Channel Name"; int importance = NotificationManager.IMPORTANCE_HIGH; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel( channelId, channelName, importance); notificationManager.createNotificationChannel(mChannel); } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(body); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addNextIntent(intent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); notificationManager.notify(notificationId, mBuilder.build()); } 
 public class MyFirebaseMessagingServices extends FirebaseMessagingService { private NotificationChannel mChannel; private NotificationManager notifManager; @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getData().size() > 0) { try { JSONObject jsonObject = new JSONObject(remoteMessage.getData()); displayCustomNotificationForOrders(jsonObject.getSsortingng("title"), jsonObject.getSsortingng("description")); } catch (JSONException e) { e.printStackTrace(); } } } private void displayCustomNotificationForOrders(Ssortingng title, Ssortingng description) { if (notifManager == null) { notifManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationCompat.Builder builder; Intent intent = new Intent(this, Dashboard.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent; int importance = NotificationManager.IMPORTANCE_HIGH; if (mChannel == null) { mChannel = new NotificationChannel ("0", title, importance); mChannel.setDescription(description); mChannel.enableVibration(true); notifManager.createNotificationChannel(mChannel); } builder = new NotificationCompat.Builder(this, "0"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(this, 1251, intent, PendingIntent.FLAG_ONE_SHOT); builder.setContentTitle(title) .setSmallIcon(getNotificationIcon()) // required .setContentText(description) // required .setDefaults(Notification.DEFAULT_ALL) .setAutoCancel(true) .setLargeIcon(BitmapFactory.decodeResource (getResources(), R.mipmap.logo)) .setBadgeIconType(R.mipmap.logo) .setContentIntent(pendingIntent) .setSound(RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION)); Notification notification = builder.build(); notifManager.notify(0, notification); } else { Intent intent = new Intent(this, Dashboard.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = null; pendingIntent = PendingIntent.getActivity(this, 1251, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setContentTitle(title) .setContentText(description) .setAutoCancel(true) .setColor(ContextCompat.getColor(getBaseContext(), R.color.colorPrimary)) .setSound(defaultSoundUri) .setSmallIcon(getNotificationIcon()) .setContentIntent(pendingIntent) .setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).bigText(description)); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1251, notificationBuilder.build()); } } private int getNotificationIcon() { boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); return useWhiteIcon ? R.mipmap.logo : R.mipmap.logo; } } 

Ceci est un bogue dans la version 11.8.0 de l’API Firebase. Donc, si vous réduisez la version de l’API, vous ne rencontrerez pas ce problème.

Si vous rencontrez un problème après avoir essayé les solutions ci-dessus, assurez-vous que l’ID de canal utilisé lors de la création du canal de notification est identique à l’identifiant de canal que vous avez défini dans le générateur Notification.

 const val CHANNEL_ID = "EXAMPLE_CHANNEL_ID" // create notification channel val notificationChannel = NotificationChannel(CHANNEL_ID, NOTIFICATION_NAME, NotificationManager.IMPORTANCE_HIGH) // building notification NotificationCompat.Builder(context) .setSmallIcon(android.R.drawable.ic_input_add) .setContentTitle("Title") .setContentText("Subtitle") .setPriority(NotificationCompat.PRIORITY_MAX) .setChannelId(CHANNEL_ID) 

Voici comment vous le faites

 private fun sendNotification() { val notificationId = 100 val chanelid = "chanelid" val intent = Intent(this, MainActivity::class.java) intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // you must create a notification channel for API 26 and Above val name = "my channel" val description = "channel description" val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(chanelid, name, importance); channel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this val notificationManager = getSystemService(NotificationManager::class.java) notificationManager.createNotificationChannel(channel) } val mBuilder = NotificationCompat.Builder(this, chanelid) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Want to Open My App?") .setContentText("Open my app and see good things") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true) // cancel the notification when clicked .addAction(R.drawable.ic_check, "YES", pendingIntent) //add a btn to the Notification with a corresponding intent val notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, mBuilder.build()); } 

Lisez le tutoriel complet à => https://developer.android.com/training/notify-user/build-notification

Application de notification Android pour Android O ainsi que les versions inférieures de l’API. Voici la meilleure application de démonstration sur GitHub-Demo 1 et GitHub-Demo 2 .

entrer la description de l'image ici

  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID") ........ NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "Hello";// The user-visible name of the channel. int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); mNotificationManager.createNotificationChannel(mChannel); } mNotificationManager.notify(notificationId, notificationBuilder.build());