首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FCM通知中没有发出任何声音

FCM通知中没有发出任何声音
EN

Stack Overflow用户
提问于 2021-02-20 21:37:42
回答 2查看 63关注 0票数 0

当用户向您发送消息时,我的应用程序发送通知,但当显示通知时,声音不会播放。贝娄是我应用程序的代码。

MyFirebaseMessaging:

代码语言:javascript
运行
复制
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    
    import androidx.annotation.NonNull;
    import androidx.core.app.NotificationCompat;
    
    import com.example.selfcial.Activities.MessageActivity;
    import com.example.selfcial.R;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    
    public class MyFirebaseMessaging extends FirebaseMessagingService {
    
        @Override
        public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
    
            String sented = remoteMessage.getData().get("sented");
    
            FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    
            if (firebaseUser != null && sented.equals(firebaseUser.getUid())) {
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    sendOreoNotification(remoteMessage);
                }else {
                    sendNotification(remoteMessage);
                }
            }
        }
    
        private void sendOreoNotification(RemoteMessage remoteMessage) {
            String user = remoteMessage.getData().get("user");
            String icon = remoteMessage.getData().get("icon");
            String title = remoteMessage.getData().get("title");
            String body = remoteMessage.getData().get("body");
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
            RemoteMessage.Notification notification = remoteMessage.getNotification();
            int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
            Intent intent = new Intent(this, MessageActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("userId", user);
            intent.putExtras(bundle);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent =  PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
    
    
            OreoNotification oreoNotification = new OreoNotification(this);
            Notification.Builder builder = oreoNotification.getOreoNotification(title, body, pendingIntent
            ,soundUri, icon);
    
            int i = 0;
            if (j > 0) {
                i = j;
            }
    
            oreoNotification.getManager().notify(i, builder.build());
        }
    
        private void sendNotification(RemoteMessage remoteMessage) {
    
            String user = remoteMessage.getData().get("user");
            String icon = remoteMessage.getData().get("icon");
            String title = remoteMessage.getData().get("title");
            String body = remoteMessage.getData().get("body");
    
            RemoteMessage.Notification notification = remoteMessage.getNotification();
            int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
            Intent intent = new Intent(this, MessageActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("userId", user);
            intent.putExtras(bundle);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent =  PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
    
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
            int i = 0;
            if (j > 0) {
                i = j;
            }
    
            notificationManager.notify(i, builder.build());
        }
    }

OreoNotification:
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.net.Uri;
import android.os.Build;

import com.example.selfcial.R;

public class OreoNotification extends ContextWrapper {

    private static final String CHANNEL_ID = "some_id";
    private static final String  CHANNEL_NAME= "Selfcial";

    private NotificationManager notificationManager;

    public OreoNotification(Context base) {
        super(base);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT);

        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(channel);
    }

    public  NotificationManager getManager() {
        if (notificationManager == null) {
            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        }

        return notificationManager;
    }

    @TargetApi(Build.VERSION_CODES.O)
    public  Notification.Builder getOreoNotification(String title,
                                                     String body,
                                                     PendingIntent pendingIntent,
                                                     Uri soundUri,
                                                     String icon) {

        return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
                .setContentIntent(pendingIntent)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_notification)
                .setSound(soundUri)
                .setAutoCancel(true);
    }
}

可能有什么问题?我想我可能忘记了触发MyFirebaseMessaging上的Uri,但我做到了。我也尝试添加一个自定义的声音,因为我认为它可能是一个带有默认声音的bug,但是没有什么改变。还有别的办法让它发生吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-21 20:57:01

.setSound(soundUri)对Android和更新版不做任何操作。

对于较新的Android版本,通知声音依赖于声道,并配置在Android设置中,应用程序>>,应用程序>>通知

票数 0
EN

Stack Overflow用户

发布于 2021-02-21 13:58:57

下面的代码片段可能会帮助您解决问题。每当通知到达时,此片段将生成默认通知声音。

代码语言:javascript
运行
复制
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder ( this, NOTIFICATION_CHANNEL_ID );
            notificationBuilder.setAutoCancel ( true )
                    .setStyle ( new NotificationCompat.BigTextStyle ().bigText ( remoteMessage.getNotification ().getBody () ) )
                    .setDefaults ( Notification.DEFAULT_ALL )
                    .setWhen ( System.currentTimeMillis () )
                    .setSmallIcon ( R.drawable.ic_notification_icon )
                    .setTicker ( remoteMessage.getNotification ().getTitle () )
                    .setPriority ( Notification.PRIORITY_MAX )
                    .setContentTitle ( remoteMessage.getNotification ().getTitle () )
                    .setContentText ( remoteMessage.getNotification ().getBody () )
                    .setContentIntent ( contentIntent );
            notificationManager.notify ( 1, notificationBuilder.build () );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66296820

复制
相关文章

相似问题

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