2

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.

Betsy
  • 219
  • 3
  • 10

3 Answers3

0

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

Rahul
  • 241
  • 1
  • 12
0

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.

Community
  • 1
  • 1
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
0

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 { 
         
    }
 }
woodruff
  • 16
  • 2