微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通知推送没有声音,振动,默认情况下为Android

如何解决通知推送没有声音,振动,默认情况下为Android

我已经开发了用于Firebase进行推送的Android应用程序,我希望使用设备的认声音发送推送通知。我已经在android 6上测试了该应用程序,并且可以正常工作,它显示了图标,标题,正文并具有声音。但是,当我将推送发送到android 9设备时,它收到了推送,但没有声音,振动,led .....仅标题和正文。能告诉我如何在Android 9的频道上设置亮度,振动和声音的所有标志吗?我检查了电话设置,可以看到认情况下如何禁用应用程序,并且仅启用声音通道,并且我想像其他应用程序一样认启用它 谢谢

代码部分。

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);

解决方法

添加这些权限

    <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。添加构建方法

  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。显示通知

  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,intent,PendingIntent.FLAG_ONE_SHOT or
                        PendingIntent.FLAG_UPDATE_CURRENT
            )
            builder.setContentIntent(pIntent)
            manager.notify(757,builder.build())


如果仍然无法运行,请卸载并重新安装该应用程序

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。