[expo-notifications][managed workflow][EAS Build][Android] custom sound not playing in locally scheduled notifications

Viewed 388

I am scheduling notifications locally with custom sound but custom sound is not playing. Infact no notification alert is shown. even though show alert and shouldPlaySound is set to true in setNotificationsHandler. It should also be mentioned that color also remains the same even though i have added the custom color in expo-notifications plugin in app.json as well as notification channel and notification content input.

i checked the notification settings of the device in which i installed the apk. notification channel is present and default ringtone of this channel is also set to the custom sound. however it just doesn’t play when the notification comes.

Relevant Code:

import { StatusBar } from "expo-status-bar";
import React from "react";
import { Text, View, Platform, Button } from "react-native";
import * as Notifications from "expo-notifications";

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});

export default function App() {
  React.useEffect(() => {
    setNotificationChannelAsync();
  }, []);

  return (
    <View
      style={{
        flex: 1,
        alignItems: "center",
        justifyContent: "space-around",
      }}
    >
      <Text>This App is for Testing Notifications</Text>
      <Button
        title="Press to schedule a notification"
        onPress={async () => {
          await scheduleNotification();
        }}
      />
      <StatusBar style="auto" />
    </View>
  );
}

const setNotificationChannelAsync = () => {
  if (Platform.OS === "android") {
    Notifications.setNotificationChannelAsync("sound", {
      name: "sound notification",
      importance: Notifications.AndroidImportance.HIGH,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: "#FF231F7C",
      sound: "adhan.wav",
    });
  }
};

async function scheduleNotification() {
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "You've got mail! ",
      body: "Here is the notification body",
      data: { data: "goes here" },
      sound: "adhan.wav",
      color: "#FF231F7C",
    },
    trigger: { seconds: 5, channelId: "sound" },
  });
}

following is the github repo of minimum reproducible example. https://github.com/basit3407/testing-custom-sound-notifications

2 Answers

I think it was because of the default notification channel. I deleted all the channels. Set the new new channel with new channel identifier and it started working.

Okay, for whatever reason, the android notification will only play if you set the vibrate property to false when you schedule your notification like so:

  async function scheduleNotification() {
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "You've got mail! ",
      body: "Here is the notification body",
      data: { data: "goes here" },
      sound: "adhan.wav",
      color: "#FF231F7C",
      vibrate: false,


  },
    trigger: { seconds: 5, channelId: "sound" },
  });
}
Related