首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android;FCM推送通知不播放声音

Android;FCM推送通知不播放声音
EN

Stack Overflow用户
提问于 2018-06-08 15:17:49
回答 1查看 2.1K关注 0票数 0

我正在从我们的应用服务器发送数据消息,调用firebase api到users.Users,我们正在接收通知,但当通知到达时,声音没有播放。

以下是代码

代码语言:javascript
复制
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();
    }
}

问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 15:40:03

您需要在通知生成器上设置声音。所以,试着在下面的行中取消注释。

代码语言:javascript
复制
.setSound(defaultSoundUri)

并在下面的行下注释。

代码语言:javascript
复制
//Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
//r.play(); 

如果你想在app后台播放通知声音,你需要add the sound parameter到通知负载。

支持默认或捆绑在应用中的声音资源的文件名。声音文件必须位于/res/raw/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50755365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档