Alexandre C. do Carmo Alexandre C. do Carmo - 8 months ago 55
Swift Question

Send remote push notification to specific user

I created a code to send remote notification using APNs, I created a development certificate, ssl, etc. The code worked very well, when I call my php script on my server, the script send a notification and my app on my iPhone show the notification.
But I have a doubt, this is a generic code, so this code will send a nofication to all iphones that has my app, but if I need to send a notification to a specific user? Like whatsapp for example? How can I inform to APNs what user that should receive the notification?


You will need either a new table to handle the user_id and its device token or add a column to the user table for device token. When you send a push notification, you can then send directly to the user device token which alert the user.

Some steps that you need to prepare will be a api endpoint to allow user to PUT or POST its device token to its account. Maybe something like api/v1/user/11/token. In your iOS side, you will then just call this endpoint to send the device token to the endpoint when the permission is accept.

I would suggest a separate table to handle the token because some user have different device like android and iOS. So if you want to alert both devices, is will be a lot easier to manage.