且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS推送通知给特定用户?

更新时间:2023-12-03 16:18:22

是的,您绝对可以向特定设备发送推送通知.

Yes, you can absolutely send a push notification to a specific device.

首先您需要向设备请求接收推送通知的权限:

First you need to ask the device for permission to receive push notifications:

[[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                     UIRemoteNotificationTypeSound |
                                     UIRemoteNotificationTypeAlert)]; 

然后如果用户接受,您将收到委托消息 didRegisterForRemoteNotificationsWithDeviceToken.然后,您将该 deviceToken 与您的应用程序中的用户 ID 一起传递到您的服务器,该用户 ID 用于标识您论坛中的用户.像这样:

Then if the user accepts you will receive the delegate message didRegisterForRemoteNotificationsWithDeviceToken. You then pass that deviceToken to your server with the user Id from your app that identifies the user in your forum. Something like this:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {

    NSString* deviceToken = [[[[_deviceToken description]
                            stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                            stringByReplacingOccurrencesOfString: @">" withString: @""] 
                            stringByReplacingOccurrencesOfString: @" " withString: @""];

   //send deviceToken and your userId to your server to associate the two together 
}

然后你需要构建你的推送服务器来发送通知,我不会在这里介绍,因为有大量的在线文档,而且非常复杂.您也可以为此使用第三方服务,例如 Urban Airship 和 StackMob.

Then you need to build your push server which sends the notifications, which I won't get into here as there is a ton of documentation online for that and it is quite involved. You can also use third party services for this such as Urban Airship and StackMob.