且构网

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

IOS推送通知给特定的用户?

更新时间:2023-12-03 16:36:10

是的,你绝对可以发送推送通知到一个特定的设备。

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。事情是这样的:

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 
}

然后,你需要建立它发送通知,我不会进入这里因为有一吨的文档在线的,这是相当复杂的推送服务器。您也可以为这个如城市飞艇和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.