如何从设置面板获取更改通知中的设备令牌

时间:2019-03-27 16:54:34

标签: react-native

使用PushNotificationIOS库,我可以在register事件上获取deviceToken:

    const registerPN = PushNotificationIOS.addEventListener('register', deviceToken => {
      console.log("Registering Push Notification Token", deviceToken);
      store.dispatch(setPushNotificationToken(userPushNotificationToken));
    });

但是,如果用户最初拒绝通知问题,我们会通过以下方式推动他们更新设置:

Alert.alert(
  'Action Required', 
  `To turn notifications on, you'll need to update your settings`,
  [
    {
      text: 'Go to settings',
      onPress: () => {
        Linking.openURL('app-settings:');
      }
    },
    {
      text: 'Cancel', 
      style: 'cancel'
    }
  ]
)

如果用户随后转到设置面板并返回,则似乎未调用“ register”事件,因此我不确定如何获取设备令牌。

如何为“设置”更新添加侦听器,以便可以存储用于服务器推送的令牌?

1 个答案:

答案 0 :(得分:0)

想通了。

使用PushNotificationIOS.checkPermissions()调用,可以检查它们是否已更改。如果是这样,请触发一个PushNotificationIOS.requestPermissions()调用,该调用将自动使用正确的令牌触发侦听器。

相关问题