XCode iDevice app - How to listen for message/request from server?

蓝咒 提交于 2019-12-25 14:10:28

问题


I have a project where I need to be able to send messages from a server running a web service to a specific iDevice. I have no idea how to do this, so any help is very appreciated.

The scenario: I have a web service which receives some message from an iDevice (could also be a Mac or PC, even an Android device). Depending on the content of this message I need to be able to send a message from the web service to another iDevice (I know the IP-address of the specific iDevice). I know how to use URLRequests to send a message from an iDevice to a server and collect the response from the server. In principle I could every 10 seconds send a request to the server asking if the server has any new messages to the sender (the iDevice sending the request), but I am pretty sure this is not the correct way to do it. Is there a way to have an iDevice listen for server communication on a specific port, so that the iDevice only does something active when it receives a message from the server to do something, e.g. display a message in the app listening for the server communication?

I guess I need to use something similar to the technology used for iMessage, but how is this done?

I am using XCode 4.6.2, iOS 6.1.

EDIT: Just to clarify my needs a bit more: The APNS seems to be TOO unreliable (at least that is what I have read in other threads regarding APN) as the web service in some cases can have the need for sending 2 distinct message to an iDevice within 1 minute (in some cases seconds), and as far as I have read in other threads this will simply not be possible because of how Apple's server handles ASPNS.

The app i am developing only needs to receive messages from the server when the app is active - is there any way, not using APNs, to do this, e.g. making the app listen for communication on a specific port?


回答1:


Your scenario seems pretty similar to APPLE PUSH NOTIFICATIONs (APNs).

Ideally your server side should write a code in such a way that if there is any change on server side & need to be informed to all associated devices.

Then your server should post notification Apple server which will then send a notification to all the associated devices.

Refer this link

You already have but can use this code to identify the iOS/Mac deivce

NSString *identifierString = [[NSUserDefaults standardUserDefaults] objectForKey:@"myID"];
if (!identifierString) {
    CFUUIDRef identifier = CFUUIDCreate(NULL);
    identifierString = (NSString*)CFUUIDCreateString(NULL, identifier);
    [[NSUserDefaults standardUserDefaults] setObject:identifierString forKey:@"myID"];
}
NSLog(@"%@",identifierString);

this code works till the lifetime of the app only.




回答2:


After some search I have decided not to use the APNS, because it seems like people are having all sort of experience with it. I cannot use APNS because my project needs 100 % reliability and instant communication with the server.

I have decided to use tcp communication since my project only will be used in a local network. This will obviously mean more power consumption on the iDevices, but reliability is more crucial to the project.



来源:https://stackoverflow.com/questions/16480320/xcode-idevice-app-how-to-listen-for-message-request-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!