I am unable to receive push notifications through my Apple Wallet pass. Based on the documents I have read, I have set up my APN provider and should be sending all the necessary information. Furthermore, my logs show that I am successfully sending my notification to APN, so it is confusing that no notification appears on my phone. Below are my code snippets:
apnProvider.js:
const path = require('path');
const options = {
cert: path.resolve(__dirname, '../certificates/signerCert.pem'),
key: path.resolve(__dirname, '../certificates/signerKey.pem'),
passphrase: 'test',
production: true
};
const apnProvider = new apn.Provider(options);
module.exports = apnProvider;
APN provider using an auth key (currently not being used but was previously and provided the same success message):
token: {
key: path.resolve(__dirname, '../certificates/AuthKey_627HR2YX2S.p8'),
keyId: "627HR2YX2S",
teamId: "72J45J9PH3"
},
production: true
});
API Rout:
const { userId } = req.body;
console.log(`Received POST request on /api/send-notification for user`);
try {
console.log(`Sending notification to user: ${userId}`);
const user = await User.findById(userId);
if (user && user.deviceToken) {
console.log('User Name:', user.firstName, user.lastName);
console.log('Device Token:', user.deviceToken);
let notification = new apn.Notification();
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
notification.badge = 1;
notification.sound = 'default';
notification.alert = 'Hello World';
notification.payload = { messageFrom: 'Kudjo' };
notification.topic = 'pass.com.kudjo';
console.log(`Sending notification to device token: ${user.deviceToken}`);
console.log(`Notification payload:`, notification.payload);
apnProvider.send(notification, user.deviceToken).then(response => {
console.log('Push notification sent:', response);
res.status(200).json({ message: 'Notification sent' });
}).catch(error => {
console.error('Error sending push notification:', error);
res.status(500).json({ message: 'Error sending notification One' });
});
} else {
console.log('User or device token not found');
res.status(404).json({ message: 'User or device token not found' });
}
} catch (error) {
console.log('Error sending notification:', error);
res.status(500).json({ message: 'Error sending notification Two' });
}
});
Thank you for your help!