Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is notification channel id?notifications not work in api 27 [duplicate]

I updated my project api target to 27 and all notifications were disabled.
What's the difference between notification in api 26 and 27?

        Notification notif = new NotificationCompat.Builder(this)                 .setContentTitle(getString(R.string.app_name))                 .setContentText(message)                 .setSmallIcon(R.mipmap.ic_launcher)                 .setContentIntent(pIntent)                 .setSound(alarmSound)                 .setAutoCancel(true).build();         notif.ledARGB = 0xFFff0000;         notif.flags = Notification.FLAG_SHOW_LIGHTS;         notif.ledOnMS = 100;         notif.ledOffMS = 100;          NotificationManager notificationCompatManager =                 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);         notificationCompatManager.notify(0, notif); 
like image 448
AmirHossein Teymoori Avatar asked Nov 21 '17 09:11

AmirHossein Teymoori


People also ask

What is a notification channel ID?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification. Builder (line 7) when constructing the Notification object. NotificationChannel settings, except channel name and description text, are immutable after it is submitted to NotificationManager at line 5.

What are channels in notification?

What Are Notification Channels on Android? “Notification Channels” were introduced in 2017 with Android 8.0 Oreo. The idea is that apps can group different types of notifications into “channels.” Each channel can then be turned on or off by the user. This all happens in the Android settings.

How do I set notification channels on Android?

To create a notification channel, follow these steps: Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level. Optionally, specify the description that the user sees in the system settings with setDescription() .

What is system silent channel notification?

Silent: Your phone won't make a sound or vibrate. But the notification will show up when you swipe down from the top of your screen.


1 Answers

As stated in the Android developers website:

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.

So You can use this method to show notification in both -27 and +27 apis :

Java :

    void showNotification(String title, String message) {     NotificationManager mNotificationManager =             (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {         NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",                 "YOUR_CHANNEL_NAME",                 NotificationManager.IMPORTANCE_DEFAULT);         channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DESCRIPTION");         mNotificationManager.createNotificationChannel(channel);     }     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "YOUR_CHANNEL_ID")             .setSmallIcon(R.mipmap.ic_launcher) // notification icon             .setContentTitle(title) // title for notification             .setContentText(message)// message for notification             .setAutoCancel(true); // clear notification after click     Intent intent = new Intent(getApplicationContext(), ACTIVITY_NAME.class);     PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);     mBuilder.setContentIntent(pi);     mNotificationManager.notify(0, mBuilder.build()); } 

Kotlin :

fun showNotification(title: String, message: String) {     val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {         val channel = NotificationChannel("YOUR_CHANNEL_ID",                 "YOUR_CHANNEL_NAME",                 NotificationManager.IMPORTANCE_DEFAULT)         channel.description = "YOUR_NOTIFICATION_CHANNEL_DESCRIPTION"         mNotificationManager.createNotificationChannel(channel)     }     val mBuilder = NotificationCompat.Builder(applicationContext, "YOUR_CHANNEL_ID")             .setSmallIcon(R.mipmap.ic_launcher) // notification icon             .setContentTitle(title) // title for notification             .setContentText(message)// message for notification             .setAutoCancel(true) // clear notification after click     val intent = Intent(applicationContext, ACTIVITY_NAME::class.java)     val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)     mBuilder.setContentIntent(pi)     mNotificationManager.notify(0, mBuilder.build()) } 

Note: If you want to show heads-up notification, you can set the importance of your channel and your notification as HIGH, AND remove your app and install it again.

like image 177
Pouya Heydari Avatar answered Oct 05 '22 20:10

Pouya Heydari