触发无声音的推送通知

时间:2018-09-06 05:42:20

标签: javascript android firebase push-notification firebase-cloud-messaging

我正在开发基于推送通知(自定义通知)的应用程序。我正在使用FCMjavascript (index.js)。我能够完美地触发推送通知,但问题是合理的。我希望在一种情况下(if)带有声音的推送通知,而在另一种情况下(else)没有声音的推送通知。发生的事情是,在两种情况下我都声音良好。如何在没有声音的情况下触发推送通知?

String remoteMessageSound = remoteMessageData.get(REMOTE_SOUND);
            if (remoteMessageSound.equals("default")) {
                Notification notificationDefault = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                        .setSmallIcon(R.drawable.namma_apartment_notification)
                        .setAutoCancel(true)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(message)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setPriority(PRIORITY_DEFAULT)
                        .build();

                Objects.requireNonNull(notificationManager).notify(mNotificationID, notificationDefault);
            } else {
                Notification notificationNoSound = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                        .setSmallIcon(R.drawable.namma_apartment_notification)
                        .setAutoCancel(true)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(message)
                        .setSound(null)
                        .setPriority(PRIORITY_LOW)
                        .build();

                Objects.requireNonNull(notificationManager).notify(mNotificationID, notificationNoSound);
            }

这是我的index.js:

if (cabNotificationSound) {
            payload = {
                data: {
                    message: "Your Cab has " + status + " your society.",
                    "sound": "default",
                    type: "Cab_Notification"
                }
            };
        }
        else {
            payload = {
                data: {
                    message: "Your Cab has " + status + " your society.",
                    "sound": "",
                    type: "Cab_Notification"
                }
            };
        }


        return admin.messaging().sendToDevice(tokenId, payload).then(result => {
            return console.log("Notification sent");
        });     

1 个答案:

答案 0 :(得分:0)

在发送通知之前,尝试类似这样的操作直接在通道中设置声音(对于api> = 26 ):

NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(getString(R.string.default_notification_channel_id));
channel.setSound(null, null);
相关问题