pushofy 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +172 -3
- data/bin/pushofy.rb +2 -2
- data/example/test.rb +2 -0
- data/lib/pushofy/push.rb +15 -14
- data/lib/pushofy/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d179637cd187441c536b4a65bb887a9078f29026
|
4
|
+
data.tar.gz: e25e9da3bb731fa28e0cb4f51c40fe101833a8d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65fc9ce6c78df6dfd89329b5cbfe942b214a0fc06054028dc09d822d8e043f40b2a9895d1c2a6049954ab1321fd90f774a769bfc02887d2d844a596f02395511
|
7
|
+
data.tar.gz: fc75473bcd14bcbf932f8a82222d02521cb8ff24f52c23b76c30dee9eef9201a134cb18190a3e49e7fbfa110c186fb6121d0031833d6f5487562edf6c6765ef8
|
data/README.md
CHANGED
@@ -1,6 +1,42 @@
|
|
1
1
|
# Pushify
|
2
2
|
|
3
|
-
|
3
|
+
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.
|
4
|
+
|
5
|
+
The basic idea behind this gem is that each device has a unique device id.
|
6
|
+
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.
|
7
|
+
|
8
|
+
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
|
9
|
+
```
|
10
|
+
device_id:12345485459, device_type:Android, device_token:sdhbfdsfbdjkbkjfbdksljbfdskjbfs.
|
11
|
+
```
|
12
|
+
|
13
|
+
So when you want to send a notification to particular user, you only call something like
|
14
|
+
```
|
15
|
+
uri = URI('http://developer.idlecampus.com/push/push1')
|
16
|
+
headers = { 'Content-Type' => 'application/json' }
|
17
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
18
|
+
resp, _ = http.post(uri.path, hash.to_json, headers)
|
19
|
+
```
|
20
|
+
|
21
|
+
where hash is
|
22
|
+
```ruby
|
23
|
+
entries_hash = {}
|
24
|
+
entries_hash['devices'] = devices
|
25
|
+
entries_hash['message'] = @message
|
26
|
+
entries_hash['app'] = @app
|
27
|
+
entries_hash['from'] = @from
|
28
|
+
timetable_hash['push'] = entries_hash
|
29
|
+
```
|
30
|
+
|
31
|
+
where devices are all the unique ids you get from the database.
|
32
|
+
|
33
|
+
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.
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
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.
|
38
|
+
|
39
|
+
Feel free to fork the code to contribute to the project as shown at the bottom of this file.
|
4
40
|
|
5
41
|
## Installation
|
6
42
|
|
@@ -10,7 +46,7 @@ Add this line to your application's Gemfile:
|
|
10
46
|
|
11
47
|
And then execute:
|
12
48
|
|
13
|
-
|
49
|
+
$ bundle
|
14
50
|
|
15
51
|
Or install it yourself as:
|
16
52
|
|
@@ -18,7 +54,140 @@ Or install it yourself as:
|
|
18
54
|
|
19
55
|
## Usage
|
20
56
|
|
21
|
-
|
57
|
+
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.
|
58
|
+
|
59
|
+
### Apple
|
60
|
+
```
|
61
|
+
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
|
62
|
+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
63
|
+
NSString *uuidString = [defaults objectForKey: @"device_identifier"];
|
64
|
+
if (!uuidString)
|
65
|
+
{
|
66
|
+
uuidString = (NSString *) CFUUIDCreateString (NULL, CFUUIDCreate(NULL));
|
67
|
+
[defaults setObject: uuidString forKey: @"device_identifier"];
|
68
|
+
[defaults synchronize]; // handle error
|
69
|
+
}
|
70
|
+
NSString *deviceToken = [[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
|
71
|
+
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
|
72
|
+
[self sendProviderDeviceToken:deviceToken device_identifier:uuidString];
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
```
|
78
|
+
-(void)sendProviderDeviceToken:(NSString *)registration_Id device_identifier:(NSString *)device_identifier {
|
79
|
+
NSURL *aUrl = [NSURL URLWithString:@"http://developer.idlecampus.com/devices"];
|
80
|
+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
|
81
|
+
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
|
82
|
+
[request setHTTPMethod:@"POST"];
|
83
|
+
NSString *postString = [NSString stringWithFormat:@"registration_id=%@&device_identifier=%@&device_type=IOS",registration_Id,device_identifier];
|
84
|
+
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
|
85
|
+
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
|
86
|
+
delegate:self];
|
87
|
+
}
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
### Android
|
92
|
+
|
93
|
+
Use the code below to send your device id to http://developer.idlecampus.com.
|
94
|
+
|
95
|
+
String serverUrl = "http://developer.idlecampus.com/devices";
|
96
|
+
Log.i("idlecampus",serverUrl);
|
97
|
+
Map<String, String> params = new HashMap<String, String>();
|
98
|
+
params.put("registration_id", regId);
|
99
|
+
|
100
|
+
String device_identifier = Settings.Secure.getString(getBaseContext().getContentResolver(), Settings.Secure.ANDROID_ID);
|
101
|
+
|
102
|
+
params.put("device_identifier",device_identifier);
|
103
|
+
params.put("device_type","Android");
|
104
|
+
|
105
|
+
|
106
|
+
You can use this method for sending data to any server using a hashmap.
|
107
|
+
|
108
|
+
public static void post(String endpoint, Map<String, String> params)
|
109
|
+
throws IOException {
|
110
|
+
URL url;
|
111
|
+
try {
|
112
|
+
url = new URL(endpoint);
|
113
|
+
} catch (MalformedURLException e) {
|
114
|
+
throw new IllegalArgumentException("invalid url: " + endpoint);
|
115
|
+
}
|
116
|
+
StringBuilder bodyBuilder = new StringBuilder();
|
117
|
+
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
|
118
|
+
// constructs the POST body using the parameters
|
119
|
+
while (iterator.hasNext()) {
|
120
|
+
Entry<String, String> param = iterator.next();
|
121
|
+
bodyBuilder.append(param.getKey()).append('=')
|
122
|
+
.append(param.getValue());
|
123
|
+
if (iterator.hasNext()) {
|
124
|
+
bodyBuilder.append('&');
|
125
|
+
}
|
126
|
+
}
|
127
|
+
String body = bodyBuilder.toString();
|
128
|
+
byte[] bytes = body.getBytes();
|
129
|
+
HttpURLConnection conn = null;
|
130
|
+
try {
|
131
|
+
conn = (HttpURLConnection) url.openConnection();
|
132
|
+
conn.setDoOutput(true);
|
133
|
+
conn.setUseCaches(false);
|
134
|
+
conn.setFixedLengthStreamingMode(bytes.length);
|
135
|
+
conn.setRequestMethod("POST");
|
136
|
+
conn.setRequestProperty("Content-Type",
|
137
|
+
"application/x-www-form-urlencoded;charset=UTF-8");
|
138
|
+
// post the request
|
139
|
+
OutputStream out = conn.getOutputStream();
|
140
|
+
out.write(bytes);
|
141
|
+
out.close();
|
142
|
+
// handle the response
|
143
|
+
int status = conn.getResponseCode();
|
144
|
+
if (status != 200) {
|
145
|
+
throw new IOException("Post failed with error code " + status);
|
146
|
+
}
|
147
|
+
} finally {
|
148
|
+
if (conn != null) {
|
149
|
+
conn.disconnect();
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
but if you want to design an app to send a push to user then register users along with their device ids
|
157
|
+
|
158
|
+
### Apple
|
159
|
+
|
160
|
+
NSString *cuser = [NSString stringWithCString:user.c_str()
|
161
|
+
encoding:[NSString defaultCStringEncoding]];
|
162
|
+
NSString *cemail = [NSString stringWithCString:email.c_str()
|
163
|
+
encoding:[NSString defaultCStringEncoding]];
|
164
|
+
NSString *urlString = [NSString stringWithFormat:@"http://idlecampus.com/users/%@",cuser];
|
165
|
+
NSURL *aUrl = [NSURL URLWithString:urlString];
|
166
|
+
HTTPDelegate *dd = [[HTTPDelegate alloc] init];
|
167
|
+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
168
|
+
NSString *uuidString = [defaults objectForKey:@"device_identifier"];
|
169
|
+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
|
170
|
+
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
|
171
|
+
[request setHTTPMethod:@"POST"];
|
172
|
+
NSString *postString = [NSString stringWithFormat:@"email=%@&jabber_id=%@&user[device_identifier]=%@&_method=%@", cemail,cuser, uuidString,@"PUT"];
|
173
|
+
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
|
174
|
+
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
|
175
|
+
delegate:dd];
|
176
|
+
}
|
177
|
+
|
178
|
+
### Android
|
179
|
+
|
180
|
+
with params and url as http://idlecampus.com/api/users
|
181
|
+
|
182
|
+
```java
|
183
|
+
Map<String, String> params = new HashMap<String, String>();
|
184
|
+
String device_identifier = settings.getString("device_identifier", "");
|
185
|
+
params.put("device_identifier", device_identifier);
|
186
|
+
params.put("jabber_id", name + "@idlecampus.com");
|
187
|
+
params.put("email", email);
|
188
|
+
params.put("name", name);
|
189
|
+
params.put("password", password);
|
190
|
+
```
|
22
191
|
|
23
192
|
## Contributing
|
24
193
|
|
data/bin/pushofy.rb
CHANGED
@@ -11,7 +11,7 @@ require "active_support/core_ext"
|
|
11
11
|
# choice = gets.to_i
|
12
12
|
|
13
13
|
# if choice == 1
|
14
|
-
# puts "Enter the device token of the iOS device:"
|
14
|
+
# puts "Enter the device token of the iOS device:"
|
15
15
|
# device_token = gets.chomp
|
16
16
|
# puts "Enter the certificate name >"
|
17
17
|
# cert_name = gets.chomp
|
@@ -64,7 +64,7 @@ payload_hash['url'] = "dsfdfdsf"
|
|
64
64
|
payload_hash['device'] = "A078F1658F68521A278D0F5C41B9C4B0613E4A6DDC157FD8ED1567CDA5634DA4"
|
65
65
|
# puts payload_hash
|
66
66
|
settings = {
|
67
|
-
cert: '/Users/
|
67
|
+
cert: '/Users/pradeepkothari/Documents/workspace/pushofy/DevCert.pem',
|
68
68
|
password: "akk322",
|
69
69
|
host: 'gateway.sandbox.push.apple.com',
|
70
70
|
port: 2195
|
data/example/test.rb
ADDED
data/lib/pushofy/push.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require "pushofy/version"
|
2
1
|
require 'rubygems'
|
3
2
|
require 'socket'
|
4
3
|
require_relative 'applepayload'
|
@@ -20,26 +19,28 @@ module Pushofy
|
|
20
19
|
def send_push
|
21
20
|
device = Device.find(@id)
|
22
21
|
device_type = device.device_type
|
23
|
-
if device_type == 'IOS'
|
22
|
+
if device_type == 'IOS'
|
24
23
|
send_to_ios(device)
|
25
|
-
elsif device_type == 'Android'
|
24
|
+
elsif device_type == 'Android'
|
26
25
|
send_to_android(device)
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
30
29
|
def send_to_ios(device)
|
31
30
|
|
32
|
-
|
33
|
-
payload_hash
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
31
|
+
|
32
|
+
payload_hash = {
|
33
|
+
aps: {
|
34
|
+
alert: {
|
35
|
+
body: "You have a new #{@app}, form #{@from}"
|
36
|
+
},
|
37
|
+
sound: 'default',
|
38
|
+
badge: 1,
|
39
|
+
},
|
40
|
+
app: @app,
|
41
|
+
url: @message
|
42
|
+
}
|
43
|
+
|
43
44
|
ApplePush.new(payload_hash, device_token_hex).push
|
44
45
|
end
|
45
46
|
|
data/lib/pushofy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pushofy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ankur Kothari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- Rakefile
|
87
87
|
- bin/pushofy.rb
|
88
88
|
- example/CertificateName.pem
|
89
|
+
- example/test.rb
|
89
90
|
- lib/pushofy.rb
|
90
91
|
- lib/pushofy/DevCert.pem
|
91
92
|
- lib/pushofy/androidpush.rb
|
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
122
|
version: '0'
|
122
123
|
requirements: []
|
123
124
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.1
|
125
|
+
rubygems_version: 2.4.1
|
125
126
|
signing_key:
|
126
127
|
specification_version: 4
|
127
128
|
summary: Sending push notifications to Android and iOS devices.
|