Notification push without sound, vibration and led by default android

Add these permissions

    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

2.Add build method

  fun createBuilder(
        context: Context,
        manager: NotificationManagerCompat): NotificationCompat.Builder {
        val channelId = context.packageName
        val notBuilder = NotificationCompat.Builder(context, channelId)
        notBuilder.setSmallIcon(R.drawable.ic_stat_name)
        notBuilder.setAutoCancel(true)
        notBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        notBuilder.setDefaults(Notification.DEFAULT_ALL)
        notBuilder.priority = NotificationCompat.PRIORITY_HIGH
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        notBuilder.setSound(soundUri)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance= NotificationManager.IMPORTANCE_HIGH 
            val channel = NotificationChannel(
                channelId, "Notifications",
                importance
            )
            channel.importance =importance
            channel.shouldShowLights()
            channel.lightColor = Color.BLUE
            channel.canBypassDnd()
            val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build()
            channel.setSound(soundUri, audioAttributes)
            channel.description = "Enable Notification"
            notBuilder.setChannelId(channelId)
            manager.createNotificationChannel(channel)
        }
        return notBuilder
    }

3.Show notification

  val builder = createBuilder(context, manager)
            builder.setContentTitle(title)
            builder.setContentText(message)
            builder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
    val parentIntent = Intent(context, MainActivity::class.java)
            parentIntent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TASK
                    or Intent.FLAG_ACTIVITY_NEW_TASK)
 val pIntent = PendingIntent.getActivity(
                context, 0,
                intent,
                PendingIntent.FLAG_ONE_SHOT or
                        PendingIntent.FLAG_UPDATE_CURRENT
            )
            builder.setContentIntent(pIntent)
            manager.notify(757, builder.build())


If,it Still not works.Uninstall and reinstall the app

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top