I am having an issue I've been trying to debug for few days and lost hope. I have an Ionic mobile app with a Firebase backend. I am trying to write a Twilio chat. I'm trying to get it to work for iOS for now. This is what I've done so far:
Why am I not receiving the notification when the app is not open? The logic is clearly working because the Firebase SDK does receive the notification when I open the app. It also doesn't seem to be a problem with any app settings because I was able to get the notification when the app is in the background when sending the notification directly from Firebase.
This is my backend code that generates the Twilio access token:
// Twilio credentials and ids are defined here...
const chatGrant = new ChatGrant({
serviceSid: twilioServiceSid,
pushCredentialSid: credentialSid
});
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret, {
identity: uid
});
token.addGrant(chatGrant);
return token.toJwt();
This is a snippet from my Ionic code that gets the FCM token from the device and passes it to Twilio
async getToken(): Promise<string> {
let token: string;
if (this.platform.is('ios')) {
const hasPermission = await this.firebaseNative.hasPermission();
if (!hasPermission) {
try {
await this.firebaseNative.grantPermission();
} catch (e) {
console.error('Error granting permission', e);
throw e;
}
}
try {
token = await this.firebaseNative.getToken();
} catch (e) {
console.error('Error getting FCM token', e);
throw e;
}
}
return token;
}
setPushRegistrationId(token: string) {
return this.twilioClient.setPushRegistrationId('fcm', token);
}
getToken().then(async (token) => {
console.log('fcm token: ' + token);
setPushRegistrationId(token)
.then(() => console.log('Added registration token'))
.catch(e => console.error('Error registering fcm token', e));
}).catch(e => {
console.error('Error getting FCM token', e);
console.error(e);
});
Please help! Thank you 😊
I recently discovered that Twilio sends only data pushes for FCM. That means that you need to handle background pushes and decide either to show them locally or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With