简体   繁体   English

通知自定义声音在前台播放,但在后台应用程序时不播放

[英]Notification custom sound play in foreground but not playing while app in background

I am adding custom sound for my application.我正在为我的应用程序添加自定义声音。 The custom ringtone is playing when the application is in the foreground but the custom sound is not playing when the application is in background.当应用程序在前台时正在播放自定义铃声,但当应用程序在后台时不播放自定义声音。

  private void sendMyNotification(String message) {

    Intent intent = new Intent(this, BaseActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.ring);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
            .setSmallIcon(R.mipmap.iconfinal)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        if(soundUri != null){
            // Changing Default mode of notification
            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            // Creating an Audio Attribute
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();

            // Creating Channel
            NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setSound(soundUri,audioAttributes);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
    }
    mNotificationManager.notify(1, notificationBuilder.build());
}

First you need to pass your sound file name from server side.首先,您需要从服务器端传递您的声音文件名。 like below example:像下面的例子:

{
  "to": "firebase_notification_token",
  "notification": {
    "title": "Push notification title",
    "body": "Push body",
    "sound": "filename", <-- point to src/res/raw/filename.mp3
  }
}

You need to add below code to play custom sound while app is in background.您需要添加以下代码才能在应用程序处于后台时播放自定义声音。

val channelId = getString(R.string.app_name)
    val NOTIFICATION_SOUND_URI =
        Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.filename)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)

val notification: Notification
notification = notificationBuilder.setSmallIcon(R.drawable.ic_logo_white).setTicker(getString(R.string.app_name)).setWhen(0)
    .setAutoCancel(true)
    .setContentTitle(getString(R.string.app_name))
    .setSound(NOTIFICATION_SOUND_URI)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentText(messageBody)
    .build()

For more detail refer to this Document .有关更多详细信息,请参阅此文档 Thanks to Ilya Eremin for great article.感谢Ilya Eremin的精彩文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 仅当应用程序处于前台时才播放推送通知声音,但当应用程序处于后台时不播放声音 - Android Push notification sound is played only when app is in foreground but not playing sound when app is in background 推送通知声音未播放应用程序在后台 - Push notification sound is not playing app is in background 自定义通知声音无法播放 - Custom notification sound not playing 当应用在后台时,Firebase推送通知不会播放声音 - Firebase Push notification is not playing sound when app is in background Android通知自定义声音不播放 - Android notification custom sound not playing Android:自定义通知声音无法播放 - Android: Custom notification sound not playing Android 应用程序 - 每小时播放(通知)声音,即使应用程序不在前台 - Android app - play (notification) sound every hour, even when the app is not in the foreground 应用程序在后台/未运行时如何设置自定义通知声音? - How to set custom notification sound when App in background/not running? FCM - 当应用被杀/在后台时,应用会收到通知但不播放通知声音 - FCM - When app is killed/in background, app receives notification but does not play the notification sound 应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同 - Push notification received while the app is in the foreground is different from push notification received when the app is in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM