简体   繁体   English

安卓; 声音未在FCM推送通知中播放

[英]Android ; Sound not playing in FCM push notifications

I am sending data messages from our app server calling firebase api to users.Users are receiving the notifications but sound is not playing when notification arrives. 我正在从应用服务器调用firebase api向用户发送数据消息。用户正在接收通知,但通知到达时声音没有播放。

here is the code 这是代码

public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    sendNotification(remoteMessage); 
}

private void sendNotification(RemoteMessage message) {
    Intent intent;
    intent = new Intent(this, HomeActivity.class);
    Log.d(TAG, message.getData().toString());

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
    r.play();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(message.getData().get("title"))
            .setContentText(Html.fromHtml(message.getData().get("body")))
            .setAutoCancel(true)
//                .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
        saveMessage(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

what could be the problem??? 可能是什么问题呢???

You need to set sound on notification builder. 您需要在通知生成器上设置声音。 So, Try with uncomment below line. 因此,请在行下方取消注释。

.setSound(defaultSoundUri)

and comment below lines. 并在下面的行中进行注释。

//Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
//r.play(); 

If you want to play notification sound when app is in background, you need to add the sound parameter to the notification payload. 如果要在应用程序处于后台运行时播放通知声音,则需要将sound参数添加到通知有效内容中。

Supports "default" or the filename of a sound resource bundled in the app. 支持“默认”或应用程序中捆绑的声音资源的文件名 Sound files must reside in /res/raw/ 声音文件必须位于/res/raw/

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

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