IOS推送通知给特定用户?

Mat*_*eus 5 php iphone apple-push-notifications ios

是否可以向特定设备发送iOS推送通知?我已经构建了一个论坛类型的应用程序,用户可以创建一个问题,其他人可以回答它.我需要向特定用户发送iOS推送通知,该用户询问问题,通知他们问题已得到解答.这可以通过PHP或其他方法完成吗?

Joe*_*oel 9

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

首先,您需要让设备获得接收推送通知的权限:

[[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                     UIRemoteNotificationTypeSound |
                                     UIRemoteNotificationTypeAlert)]; 
Run Code Online (Sandbox Code Playgroud)

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

- (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 
}
Run Code Online (Sandbox Code Playgroud)

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