I am developing an iOS chat app. I wanted to receive the messages in the device even if the app is terminated. Also I wanted to alert the app if wifi/mobile data is turned on. How will the app send the message which was typed when wifi is off and then the app is terminated. I'm using xmpp protocol and openfire server. Please tell me how to do it, preferebly in objective C. Any useful links or tutorials is also helpful. Thanks in advance.
-
You should look into Push notification, which shows notification when app is not opened. – Saheb Roy Mar 04 '16 at 05:22
-
have you checked my answer ? – Badal Shah Mar 04 '16 at 10:25
-
@BadalShah Thanks for the reply. My problem is not when the app is in background. I wanted to know how to receive messages when the app is in killed state. – Betsy Mar 04 '16 at 13:28
-
@Babybet can you elaborate ? what you mean by killed state ? – Badal Shah Mar 05 '16 at 11:52
3 Answers
To receive the messages on the device even if the app is not opened for that you need to implement the Push Notifications.
For more details please check out the below link: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

- 241
- 1
- 12
To receive Chat message You need to use Push Notification when your app in background.
Check below link for more detail.
iphone-xmpp-app-run-background
send-push-notification-to-ios-for-chat-to-offline-user-openfire-xmpp
Basically you need to use following two methods to working with receive message.
(void)xmppStream:(XMPPStream *)sender socketWillConnect:(AsyncSocket *)socket
(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
//you will receive message here. so, set other code here what you want to do after receiving the message
You will get enough information regarding receive chat-message in background from above 2 links.
Second point , If you want to identify if App is connected with wifi or cellular data you need to use Reachability Introduction
Here is code to check connected with wifi or cellulardata.
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
//No internet
}
else if (status == ReachableViaWiFi)
{
//WiFi
}
else if (status == ReachableViaWWAN)
{
//3G // Set your code here for cellular data
}
Hope , this information is enough to understand answer of your question.

- 1
- 1

- 7,541
- 2
- 30
- 65
Question: “I wanted to receive the messages in the device even if the app is not opened”
Answer: Uh…maybe you should search “how to use Apple Push Notification Service” in Google.It's easy to find the answer
Question: “Also I wanted to alert the app if wifi/mobile data is turned on”
Answer: use Open source code :named "Reachability" on GitHub.
The use of Reachability like this in your project:
stepOne: Add "SystemConfiguration.framework"in your project
stepTwo: you should #import "Reachability.h" file on where you want to use it
stepThree: code like this
#import "XXViewController.h"
#import "Reachability.h"
@interface XXViewController ()
@property (nonatomic, strong) Reachability *conn;
@end
@implementation XXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
self.conn = [Reachability reachabilityForInternetConnection];
[self.conn startNotifier];
}
- (void)dealloc
{
[self.conn stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)networkStateChange
{
[self checkNetworkState];
}
- (void)checkNetworkState
{
// 1.your wifi
Reachability *wifi = [Reachability reachabilityForLocalWiFi];
// 2.Mobile phone can surf the Internet ability
Reachability *conn = [Reachability reachabilityForInternetConnection];
if ([wifi currentReachabilityStatus] != NotReachable) {
} else if ([conn currentReachabilityStatus] != NotReachable) {
} else {
}
}

- 16
- 2