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!
Hi @Ddud,
You wrote:
[...] Furthermore, my logs show that I am successfully sending my notification to APN, so it is confusing that no notification appears on my phone. [...]
Before we begin, I'd like to confirm that you've read the following documentation:
To send a pass update push notification, perform the following steps:
- Find the registered devices for the updated pass.
- Create and send a push notification for each registered device.
- Verify your notification matches the following criteria:
- Uses the same certificate and private key that the creator of the original used for signing.
- The push token registered by the device.
- Contains an empty JSON dictionary for the notification payload.
For invalid push token errors, delete the associated device from APNs. For more information, see Sending notification requests to APNs.
Next, you wrote:
[...] token: { [...] } [...]
Please remove sensitive information from your post. Your team identifier and private key (or key identifier) should not be made available to the public.
Then, you wrote:
Update: I confirmed that my APN provider is through "openssl s_client -connect api.sandbox.push.apple.com:443". [...]
A push notification for a pass update works only in the production environment. The sandbox environment will not deliver notifications of this type. Please attempt to send the push notification in production and report back with the results.
Cheers,
Paris