调用addPushTokenListener时如何检索expo令牌而不是ios令牌?

问题描述

我正在处理expo通知,一切正常,直到意识到通过某种方式需要通过调用Notifications.addPushTokenListener更新expo令牌,因为expo会刷新一个新的通知,而较旧的通知将停止工作。这就是我所做的。

// with this I get an expo token Ok
let token = await Notifications.getExpoPushTokenAsync();
// type expo
{
    data: "ExponentPushToken[6CoxIjM15ilo2_ZDTWzeTh]"
    type: "expo"
}

// but when I want to retrieve on refreshed token I get an IOS token
Notifications.addPushTokenListener(function(response){
    console.log(response);
});
// I get an IOS type token and I do not need that,I need an expo token eventough I am in a ios physical device
{
    data: "a9ee9433ea8a3e883cb7f5f1eb0d1bada4eed5473713153534aa2abb5cf6268f"
    type: "ios"
}

我需要的是刷新“ expo”令牌,而不是在使用expo时的IOS。

博览会推送通知是否使用相同的令牌? 是仅针对FCM / APN才需要“ addPushTokenListener”吗?

解决方法

尝试在推送令牌侦听器中调用getExpoPushTokenAsync()

import * as Notifications from 'expo-notifications';

Notifications.addPushTokenListener(response => {
  Notifications.getExpoPushTokenAsync().then(expoTokenResponse => {
    // ... do stuff
  });
});

Notifications.addPushTokenListener(async () =>
  const expoTokenResponse = await Notifications.getExpoPushTokenAsync();
  // ... do stuff
});