Search code examples
react-nativenotificationsexpo

No sound or vibration in Android push notifications in Expo Client


hi i'm new to react native I am just testing around with the push notification on android. but there is no vibration or sound when i test on my android device via expo client.

However when i open the app on my android devices via expo client, the push notification does show up, but there was no sound no vibration even though i set those to true already.

i want to make a notification than notify the user every day at 8 AM even if the app is closed can i make it ??

async componentWillMount() {
    let result = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    if (result.status === "granted" && this.state.switchStatus) {
      console.log("Notification permissions granted.");
      this.setNotifications();
    } else {
      console.log("No Permission", Constants.lisDevice);
    }

    this.listenForNotifications();
  }
  getNotification(date) {
    const localNotification = {
      title: `Notification at ${date.toLocaleTimeString()}`,
      body: "N'oubliez pas de prendre tes medicament",
      ios: {

        sound: true 
      },

      android: {
        sound: true, 
        priority: "max", 
        sticky: false, 
        vibrate: true 
      }
    };
    return localNotification;
  }
  setNotifications() {
    Notifications.cancelAllScheduledNotificationsAsync();

    for (let i = 0; i < 64; i++) {
      //Maximum schedule notification is 64 on ios.
      let t = new Date();
      if (i === 0) {
        t.setSeconds(t.getSeconds() + 1);
      } else {
        t.setMinutes(t.getMinutes() + 1 + i * 1);
      }
      const schedulingOptions = {
        time: t 
      };
      Notifications.scheduleLocalNotificationAsync(
        this.getNotification(t),
        schedulingOptions
      );
    }
  }
  listenForNotifications = () => {
    Notifications.addListener(notification => {
      console.log("received notification", notification);
    });
  };

Solution

  • i found a solution for the sound and vibration it's not the best as i said i'm new to RN but still it works ( i used this: "create Channel Android Async" )

    async componentWillMount() {
        let result = await Permissions.getAsync(Permissions.NOTIFICATIONS);
        if (result.status === "granted" && this.state.switchStatus) {
        // i add this :
          if (Platform.OS === 'android') {
      Notifications.createChannelAndroidAsync('chat-messages', {
        name: 'Chat messages',
        sound: true,
        vibrate: true,
      });
    }
          console.log("Notification permissions granted.");
    
          this.setNotifications();
        } else {
          console.log("No Permission", Constants.lisDevice);
        }
    
        this.listenForNotifications(); 
      }
    
      getNotification(date) {
    
        const localNotification = {
          title: `Notification at ${date.toLocaleTimeString()}`,
          body: "N'oubliez pas de prendre tes medicament", 
          ios: {
    
            sound: true 
          },
    
          android: {
           "channelId": "chat-messages" //and this
                }
        };
        return localNotification;
      }
      setNotifications() {
        Notifications.cancelAllScheduledNotificationsAsync();
    
        for (let i = 0; i < 64; i++) {
          //Maximum schedule notification is 64 on ios.
          let t = new Date();
          if (i === 0) {
            t.setSeconds(t.getSeconds() + 1);
          } else {
            t.setMinutes(t.getMinutes() + 1 + i * 1); // 15 Minutes
          }
          const schedulingOptions = {
            time: t 
          };
          Notifications.scheduleLocalNotificationAsync(
            this.getNotification(t),
            schedulingOptions
          );
        }
      }
      listenForNotifications = () => {
        Notifications.addListener(notification => {
          console.log("received notification", notification);
        });
      };