简体   繁体   中英

Notification push without sound, vibration and led by default android

I have developed App for android with Firebase for push and I want to send push notifications with default sound of device. I have tested app on android 6 and work correctly, it show the icon, title, body, and has sound. But when I sent the push to android 9 device, It received the push but without sound, vibration, led.... only the title and body. Could tell me how set all flags of ligth, vibration and sound on Channels on Android 9? I check the phone settings and I can see how app by default is disabled and channel enabled only the sound, and I would like to enabled it by default as other apps are doing Thank you

The part of code.

var channelId = channel.NotificationChannelId;
var channelName = channel.NotificationChannelName;
var channelImportance = channel.NotificationChannelImportance;
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var notChannel = new NotificationChannel(channelId, channelName, channelImportance);
if (SoundUri != null)
{
   try
   {
       var soundAttributes = new AudioAttributes.Builder()
           .SetContentType(AudioContentType.Sonification)
           .SetUsage(AudioUsageKind.Notification).Build();
                            notChannel.SetSound(SoundUri, soundAttributes);
   }
   catch (Exception ex)
   {
      System.Diagnostics.Debug.WriteLine(ex);
   }
}
notificationManager.CreateNotificationChannel(notChannel);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM