簡體   English   中英

通知推送默認無聲音、振動和led android

[英]Notification push without sound, vibration and led by default android

我已經為 android 和 Firebase 開發了推送應用程序,我想用設備的默認聲音發送推送通知。 我已經在 android 6 上測試了應用程序並且工作正常,它顯示圖標、標題、正文並有聲音。 但是,當我將推送發送到 android 9 設備時,它收到了推送,但沒有聲音、振動、指示燈……只有標題和正文。 可以告訴我如何在 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, 0,
                intent,
                PendingIntent.FLAG_ONE_SHOT or
                        PendingIntent.FLAG_UPDATE_CURRENT
            )
            builder.setContentIntent(pIntent)
            manager.notify(757, builder.build())


如果,它仍然不起作用。卸載並重新安裝該應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM