Expo Client中的Android推送通知中没有声音或振动

时间:2019-05-13 18:19:35

标签: react-native notifications expo

嗨,我是本机反应新手,我只是在android上进行推送通知测试。但是当我通过expo客户端在android设备上进行测试时,没有振动或声音。

但是,当我通过expo客户端在android设备上打开应用程序时,确实会显示推送通知,但是即使我已经将它们设置为true,也没有声音没有振动。

我希望发出通知,而不是每天关闭上午8点通知用户,即使该应用已关闭,我也可以发出通知吗?

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);
    });
  };

3 个答案:

答案 0 :(得分:0)

我发现了声音和振动的解决方案,这并不是最好的解决方案,因为我说我是RN的新手,但它仍然有效(我使用了此方法:“ 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);
    });
  };

答案 1 :(得分:0)

在我的情况下,我允许在(设置->通知->您的应用名称->声音->允许)中发出通知声音,并且后言效果很好

答案 2 :(得分:0)

在您的 app.json 文件中,添加

android:{useNextNotificationApi:true }