This gem has support for both APNS(sending push notifications to iOS devices) and also GCM(sending push notifications to Android Devices). This gem was extracted out from the push notification server I built for IdleCampus.
The basic idea behind this gem is that each device has a unique device id. So when the user starts his app, you send the device id along with the device type and the device token(APNS) or Registeration id(GCM) to http://developer.idlecampus.com server and within your own app you save the user along with the device id.
For example Ankur is saved to the application server database with a device id 12345485459 and in the push notification server his details get saved as
device_id:12345485459, device_type:Android, device_token:sdhbfdsfbdjkbkjfbdksljbfdskjbfs.
So when you want to send a notification to particular user, you only call something like
uri = URI('http://developer.idlecampus.com/push/push1')
headers = { 'Content-Type' => 'application/json' }
http = Net::HTTP.new(uri.host, uri.port)
resp, _ = http.post(uri.path, hash.to_json, headers)
where hash is
entries_hash = {}
entries_hash['devices'] = devices
entries_hash['message'] = @message
entries_hash['app'] = @app
entries_hash['from'] = @from
timetable_hash['push'] = entries_hash
where devices are all the unique ids you get from the database.
You can also use the website http://developer.idlecampus.com to store your certificate for you in case of APNS and the register id in case of GCM. You only have to go and create an account for yourself.
Use this gem for sending push to iOS devices and Android Devices just by uploading your certificate to http://developer.idlecampus.com and registering your devices for each user on it.
Below you will also find the iOS code and Android Code you can use in your mobile applications if you wish to leave handling the push notifications and the ceritfications to us.
Feel free to fork the code to contribute to the project as shown at the bottom of this file.
Add this line to your application's Gemfile:
gem 'pushify'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pushify
Copy the code below in your AppDelegate.m file and also upload your certificate on http://developer.idlecampus.com by creating an account if you don't want to use push notifications yourself.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uuidString = [defaults objectForKey: @"device_identifier"];
if (!uuidString)
{
uuidString = (NSString *) CFUUIDCreateString (NULL, CFUUIDCreate(NULL));
[defaults setObject: uuidString forKey: @"device_identifier"];
[defaults synchronize]; // handle error
}
NSString *deviceToken = [[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
[self sendProviderDeviceToken:deviceToken device_identifier:uuidString];
}
-(void)sendProviderDeviceToken:(NSString *)registration_Id device_identifier:(NSString *)device_identifier {
NSURL *aUrl = [NSURL URLWithString:@"http://developer.idlecampus.com/devices"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"registration_id=%@&device_identifier=%@&device_type=IOS",registration_Id,device_identifier];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
}
Use the code below to send your device id to http://developer.idlecampus.com.
String serverUrl = "http://developer.idlecampus.com/devices";
Log.i("idlecampus",serverUrl);
Map<String, String> params = new HashMap<String, String>();
params.put("registration_id", regId);
String device_identifier = Settings.Secure.getString(getBaseContext().getContentResolver(), Settings.Secure.ANDROID_ID);
params.put("device_identifier",device_identifier);
params.put("device_type","Android");
You can use this method for sending data to any server using a hashmap.
public static void post(String endpoint, Map<String, String> params)
throws IOException {
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
but if you want to design an app to send a push to user then register users along with their device ids
NSString *cuser = [NSString stringWithCString:user.c_str()
encoding:[NSString defaultCStringEncoding]];
NSString *cemail = [NSString stringWithCString:email.c_str()
encoding:[NSString defaultCStringEncoding]];
NSString *urlString = [NSString stringWithFormat:@"http://idlecampus.com/users/%@",cuser];
NSURL *aUrl = [NSURL URLWithString:urlString];
HTTPDelegate *dd = [[HTTPDelegate alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uuidString = [defaults objectForKey:@"device_identifier"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]; [request setHTTPMethod:@"POST"]; NSString *postString = [NSString stringWithFormat:@"email=%@&jabber_id=%@&user[device_identifier]=%@&_method=%@", cemail,cuser, uuidString,@"PUT"]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:dd]; }
with params and url as http://idlecampus.com/api/users
Map<String, String> params = new HashMap<String, String>();
String device_identifier = settings.getString("device_identifier", "");
params.put("device_identifier", device_identifier);
params.put("jabber_id", name + "@idlecampus.com");
params.put("email", email);
params.put("name", name);
params.put("password", password);
- Fork it ( http://github.com//pushify/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request