Hello Fellow Programmers,
I am attempting to allow users to send push notifications to other users (like sending a friend request, etc.).
The end goal here is for my iOS app to continuously listen to a specific hostname/port URL once a user has logged in to their account (aka a specific view has loaded). My stack is an express server communicating with a MongoDB.
Lets say a user with {account_id} logged in, the path to his account info would be: "http://72.89.157.153:3000/accounts/{account_id}
I would like my app to listen to any requests send to this URL. I am using GCDAsyncSocket libraries to help with the cause. However, when I connect to http://72.89.157.153:3000/ for testing purposes, no delegate function is called. I have seen many people with this same problem, but I can not get any solution I have read to pan out.
Code:
SocketConnection.h
#ifndef SocketConnection_h
#define SocketConnection_h
#import "GCDAsyncSocket.h" // for TCP
@import CocoaAsyncSocket;
@interface SocketConnection : NSObject <GCDAsyncSocketDelegate>
/* GCDAsyncSocket */
@property (strong, nonatomic) GCDAsyncSocket *socket;
// Methods
+(SocketConnection *)sharedConnection;
@end
#endif /* SocketConnection_h */
SocketConnection.m
#import <Foundation/Foundation.h>
#import "SocketConnection.h"
@implementation SocketConnection
+(SocketConnection *)sharedConnection {
static dispatch_once_t once;
static SocketConnection *instance;
dispatch_once(&once, ^{
instance = [[SocketConnection alloc] init];
});
return instance;
}
-(id)init {
_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![_socket connectToHost:@"http://72.89.157.153" onPort:3000 error:&err]) {
printf("\nDid Not Return Okay: %s\n", [[err localizedDescription] UTF8String]);
} else {
printf("\nReturned Okay\n"); // This is printed
}
return self;
}
/* ASNYC DELEGATES */
/* I am expecting this method to be called when connectToHost: is called in init.. */
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port {
printf("I'm connected! Host:%s\n", [host UTF8String]);
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
printf("I have written That was easy.\n");
}
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag {
printf("I have read That was easy.\n");
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
[_socket readDataWithTimeout:-1 tag:1];
}
});
}
@end
Here is the spot in the ViewController where I create an instance of SocketConnection...
-(void)viewDidAppear:(BOOL)animated {
/* Socket connector */
SocketConnection *s = [SocketConnection sharedConnection];
printf("port: %hu\n" ,s.socket.connectedPort); // prints 0 right now
}
If this is not the best way to achieve my goal, please point me in the right direction (link readings, other frameworks, libraries etc.) Any questions please let me know.
Thank you for the help.
okey, for your first goal (allow users to send push notifications to other users and assuming that you have a node.js server side with express and mongodb) try doing this:
First on the server side install apn and node-gcm.
This two packages are used to send push notifications to ios and android.
Once you have these packages installed make a route on your server side to send the notifications. This can be done with something like this:
Now to send a push notification you need to do a POST like this one:
IOS
Objective C
SWIFT
WEB (AJAX)
JAVA
Now you can send push notifications to all devices.
Your second goal can be done easy with your server side, when a request is sent to your URL, you can do a POST sending the push notification, for example if someone wanna add you as a friend (lets gonna say that they made a request to http://72.89.157.153:3000/friends/{account_id}), you can send a notification to the user telling him that he have a new friendship request.
Its important that's on mongodb you store the package and the token of your users, so you can send the right notifications.
Hope it helps.