Reputation: 852
I am using silent push notification in XMPP chat application. Using that I want to connect to XMPP server when app is not running or not in background mode. I am receiving silent push notification and I enable following flag of xmppStream for background connection.
xmppStream.enableBackgroundingOnSocket = YES;
But socketDidConnect method is not being still called when app is not running and can't able to connect to XMPP server.
The purpose of this is to send message received status to XMPP server. So guide me about this.
I don't want to use VoIP for this. So please provide me alternate solution for this.
Upvotes: 1
Views: 742
Reputation: 327
Follow the below steps. This is possible using VoIP and PushKit framework.
Now add PushKit.framework
In AppDelegate.h file import PushKit framework and add its delegate PKPushRegistryDelegate
AppDelegate.m file do following.
A) In didFinishLaunchingWithOptions method add below code.
PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
B) Add all delegate methods of PKPushRegistryDelegate
- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type { NSLog(@"VoIP - did Invalidate Push Token for type - %@", type); }
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSLog(@"VoIP - got Push with payload: %@ and Type: %@", payload.dictionaryPayload, type);
NSDictionary *dictAps = [payload.dictionaryPayload valueForKey:@"aps"];
if ([dictAps valueForKey:@"content-available"] != nil) {
NSLog(@"Silent VoIP");
//Fetch payload info and create local notification and fire that local notification.
UILocalNotification *voipNotification = [[UILocalNotification alloc] init];
voipNotification.alertTitle = @"Silent VoIP";
voipNotification.alertBody = [dictAps valueForKey:@"alert"];
voipNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:voipNotification];
//Call xmpp connection method here.
if (xmppStream == nil) { [self setupStream]; }
if ([xmppStream isDisconnected]) { [self connect]; }
}
}
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
NSLog(@"VoIP - device Token: %@", credentials.token);
NSString *newToken = credentials.token.description;
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"VoIP token is: %@", newToken);
[obj_DataModel setVoIPToken:newToken]; //Store token in somewhere for further use.
}
I am passing following payload for this purpose from the server.
{"aps":{"alert":"Testing...","badge":1,"sound":"default","content-available":1}}
I hope that this is helpful for you. If you have any query then ask me.
Upvotes: 2