I am trying to turn on and off push notification sounds from the server and it works well on iOS.
sound = 'default' if recipient['sounds'] is True else None
push_messages.append(PushMessage(
to=recipient['push_token'],
body=message,
data={'message': message},
sound=sound))
But my Android test machine ignores ‘sound’ property and rings notification sounds every time it receives. I read that ‘sound’ property is dismissed on Android 8+, but could not find information whether it is also dismissed for Android 7 or lower versions. To sort this out, I created a channel and sent push messages with the channel id.
sound = 'default' if recipient['sounds'] is True else None
push_messages.append(PushMessage(
to=recipient['push_token'],
body=message,
data={'message': message},
sound=sound,
channel_id='channel_id'))
componentDidMount() {
this.createChannelId();
}
createChannelId = async () => {
let sounds;
if (Platform.OS === 'android') {
sounds = await AsyncStorage.getItem('sounds');
if (sounds === null)
sounds = true;
else
sounds = (sounds === 'true');
Notifications.createChannelAndroidAsync('channel_id', {
name: 'channel_id',
sound: sounds,
});
}
}
And I deleted and re-created channel every time user changes push notification sounds setting.
onSwitchSounds = value => {
this.setState({ sounds: value }, this.onSwitchToggle);
if (Platform.OS === 'android') {
Notifications.deleteChannelAndroidAsync('channel_id');
Notifications.createChannelAndroidAsync('channel_id', {
name: 'channel_id',
sound: value,
});
AsyncStorage.setItem('sounds', value.toString());
}
}
I finally made it, but I am not sure if it is the right way. Why does sound = None not work? Is this the only way I can manage to turn on and off notification sounds by users?