向特定用户发送远程推送通知

向特定用户发送远程推送通知

问题描述:

我创建了一个使用 APNs 发送远程通知的代码,我创建了一个开发证书、ssl 等.该代码运行良好,当我在我的服务器上调用我的 php 脚本时,该脚本发送一个通知,我的应用程序在我的iPhone 显示通知.但我有一个疑问,这是一个通用代码,所以此代码将向所有拥有我的应用程序的 iphone 发送通知,但是如果我需要向特定用户发送通知?比如whatsapp?我如何通知 APNs 应该收到通知的用户?

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?

您将需要一个新表来处理 user_id 及其设备令牌,或者向用户表中添加一列用于设备令牌.当您发送推送通知时,您可以直接发送到提醒用户的用户设备令牌.

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.

您需要准备的一些步骤将是一个 api 端点,以允许用户 PUTPOST 其设备令牌到其帐户.可能类似于 api/v1/user/11/token.在您的 iOS 端,您只需调用此端点以在接受权限时将设备令牌发送到端点.

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.

我建议使用单独的表来处理令牌,因为某些用户使用不同的设备,例如 android 和 iOS.因此,如果您想同时提醒两台设备,管理起来会容易得多.

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.