【问题标题】:Notification push without sound, vibration and led by default android通知推送无声音、无振动、默认为android
【发布时间】:2021-02-10 22:39:35
【问题描述】:

我已经使用 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);

【问题讨论】:

  • 能否请您出示您正在使用的代码?
  • 我已经添加了我使用的代码片段

标签: android firebase xamarin push-notification


【解决方案1】:

添加这些权限

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


如果还是不行,请卸载并重新安装应用程序

【讨论】:

  • 好的,谢谢,我会试试的,我告诉你。一个问题 NotificationCompat.Builder 显示为已弃用,因此我没有使用它。我们还可以使用它吗?短时间内不会被删除吗?
  • @cansado2930 NotificationCompat.Builder(Context context) 已弃用,而不是 NotificationCompat.Builder(Context context, String channelID)。 stackoverflow.com/questions/45462666/…
  • 天哪!我没有看到这个区别!
  • 这是我遇到此问题时使用的。我添加了另一个权限。也尝试一下。然后卸载并安装它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多