【问题标题】:Android Firebase notification have no soundAndroid Firebase 通知没有声音
【发布时间】:2023-03-13 23:50:02
【问题描述】:

我在应用运行时收到 Firebase 通知声音,而在应用处于后台时没有收到通知声音。我不知道为什么会这样。

这是我尝试过的

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
            // To Configure the notification channel.
            notificationChannel.setDescription("Sample Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.gj512x512)
                .setContentText(message)
                .setSound(sound)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        notificationManager.notify(1, noBuilder.build());
}

请帮帮我。

【问题讨论】:

  • 您的意思是当您收到 FCM 通知时对吗?
  • 是的,这是一个 fcm 通知

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:

只需做一件事,说您的后端开发人员只需在通知中发送 data 有效负载,要求他限制和删除 通知 有效负载,

因为当您收到通知时,如果您的应用在后台,并且您当时收到通知有效负载,系统会从他们这边处理通知,这就是出现问题的原因,

因此,只需从服务器端移除 notification 有效负载,它就会正常工作。

添加您的 php 代码,如下所示以获取数据

$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
    'registration_ids'  => ['deviceID'],
    'priority' => 'high',
    'data' => array(
        'body' => $message,
        'title' => $title,
        'sound' => 'default',
        'icon' => 'icon'
    )
);

【讨论】:

  • 通知负载是什么意思?
  • 您从哪里获得 fcm 通知的第一件事?
  • 来自你的服务器?
  • 我担心的是,当您从 PHP 代码准备或发送 fcm 时,您需要更改您的有效负载或 json 数据。
  • 应用程序在前台运行时我听到声音
【解决方案2】:
 notificationChannel.setSound(null,null);  // remove this line 



.setSound(sound) //replace this line with this 

setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

【讨论】:

    【解决方案3】:

    如果您使用 FCM,则不需要实现通知代码,除非您需要为您的应用自定义它(例如特殊的灯光、特殊的声音、特殊的振动和.. .)。 否则,你也可以有一个这样的类并设置清单的东西

    FCM 类:

    public class FcmMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        showNotification(Objects.requireNonNull(remoteMessage.getNotification()).getTitle(), remoteMessage.getNotification().getBody());
    }
    
    private void showNotification(String title, String body) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "net.Test.id";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("Description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableLights(true);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(title).setContentText(body).setContentInfo("Info");
        assert notificationManager != null;
        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
    }
    
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("TOKEN", s);
    }
    }
    

    清单苍蝇:

    <service
            android:name="arbn.rahyab.rahpayadmin.data.network.fcm.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/googleg_standard_color_18" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="001" />
    

    如果要自定义,请在 FCN 类中编写代码。

    【讨论】:

    • 我正在从 php 代码发送通知,我想自定义通知声音
    • 我编辑了我的答案。还有一个自定义代码。希望对你有帮助。
    猜你喜欢
    • 2019-03-26
    • 2016-10-23
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多