webpush 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e8453b2c3a66c07ce852d302a8661cfd5f59771
4
- data.tar.gz: 97441485cfaea8a1d66f6460367758a40b593409
3
+ metadata.gz: 5c0fb7998125b81b5080ff58173609a51b8f72e7
4
+ data.tar.gz: d7dbc0144010cce8af11cbfa1ba9578aa9ef4349
5
5
  SHA512:
6
- metadata.gz: 49e9087ac49e17c2c4a23e4fcca7171aa8c6f735024e0c78328ece944d6d2b2cdda15ba72ab51d662823196b85dec23b83c170afccb7fbfe8a58e47ec81d9d75
7
- data.tar.gz: b887988ec3389600c854a9c6693bb336d1acaf9748734167eb56005c98b80392417ff37de74e7b9fc503669396cde8bdd484130f93f6f11bd217c5bc8da62392
6
+ metadata.gz: 6caaa2b268c317e331a1de19cfcc5a2cf258ba4c0c10ac9a1301785229971604087a8e2db3c168ece423cc3dcd2604ae05f9a2657a9ed739ec8bf95ec5396f94
7
+ data.tar.gz: 329f7f10e44e9d966a1f4ccc4172231fc1771dc10124182019a901530b32bf3a617614b38e9bfc525ff181ae7a6dd8023697de8fcbdbf355c2d5b42235feef07
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This Gem will send the Web Push API. It supports the encryption necessary to payload.
4
4
 
5
- Payload is supported by Chrome50+.
5
+ Payload is supported by Chrome50+, Firefox48+.
6
6
 
7
7
  ## Installation
8
8
 
@@ -24,14 +24,53 @@ Or install it yourself as:
24
24
 
25
25
  ```
26
26
  message = {
27
- hoge: "piyo"
27
+ title: "title",
28
+ body: "body",
29
+ icon: "http://example.com/icon.pn"
28
30
  }
29
31
 
30
- Webpush.payload_send(message: JSON.generate(message),
32
+ Webpush.payload_send(
33
+ message: JSON.generate(message),
31
34
  endpoint: "https://android.googleapis.com/gcm/send/eah7hak....",
32
35
  p256dh: "BO/aG9nYXNkZmFkc2ZmZHNmYWRzZmFl...",
33
36
  auth: "aW1hcmthcmFpa3V6ZQ==",
34
- api_key: "[GoogleDeveloper APIKEY]")
37
+ api_key: "[GoogleDeveloper APIKEY]" # optional, not used in Firefox.
38
+ )
39
+ ```
40
+
41
+ ### ServiceWorker sample
42
+
43
+ see. https://github.com/zaru/web-push-sample
44
+
45
+ p256dh and auth generate sample code.
46
+
47
+ ```
48
+ navigator.serviceWorker.ready.then(function(sw) {
49
+ Notification.requestPermission(function(permission) {
50
+ if(permission !== 'denied') {
51
+ sw.pushManager.subscribe({userVisibleOnly: true}).then(function(s) {
52
+ var data = {
53
+ endpoint: s.endpoint,
54
+ p256dh: btoa(String.fromCharCode.apply(null, new Uint8Array(s.getKey('p256dh')))).replace(/\+/g, '-').replace(/\//g, '_'),
55
+ auth: btoa(String.fromCharCode.apply(null, new Uint8Array(s.getKey('auth')))).replace(/\+/g, '-').replace(/\//g, '_')
56
+ }
57
+ console.log(data);
58
+ });
59
+ }
60
+ });
61
+ });
62
+ ```
63
+
64
+ payloads received sample code.
65
+
66
+ ```
67
+ self.addEventListener("push", function(event) {
68
+ var json = event.data.json();
69
+ self.registration.showNotification(json.title, {
70
+ body: json.body,
71
+ icon: json.icon
72
+ });
73
+ });
35
74
  ```
36
75
 
37
76
  ## Contributing
data/lib/webpush.rb CHANGED
@@ -12,31 +12,34 @@ module Webpush
12
12
  TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm'
13
13
 
14
14
  class << self
15
- def payload_send(message:, endpoint:, p256dh:, auth:, api_key:)
15
+ def payload_send(message:, endpoint:, p256dh:, auth:, api_key: "")
16
16
  endpoint = endpoint.gsub(GCM_URL, TEMP_GCM_URL)
17
17
  p256dh = unescape_base64(p256dh)
18
18
  auth = unescape_base64(auth)
19
19
 
20
20
  payload = encrypt(message, p256dh, auth)
21
- gcm_post(endpoint, payload, api_key)
21
+ push_server_post(endpoint, payload, api_key)
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def gcm_post(endpoint, payload, api_key)
26
+ def push_server_post(endpoint, payload, api_key = "")
27
27
  begin
28
28
  uri = URI.parse(endpoint)
29
29
  http = Net::HTTP.new(uri.host, uri.port)
30
30
  http.use_ssl = true
31
31
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
32
32
  header = {
33
- "Encryption" => "salt=#{Base64.urlsafe_encode64(payload[:salt]).delete('=')}",
34
- "Crypto-Key" => "dh=#{Base64.urlsafe_encode64(payload[:server_public_key_bn]).delete('=')}",
35
- "Authorization" => "key=#{api_key}"
33
+ "Content-Type" => "application/octet-stream",
34
+ "Content-Encoding" => "aesgcm",
35
+ "Encryption" => "salt=#{Base64.urlsafe_encode64(payload[:salt]).delete('=')}",
36
+ "Crypto-Key" => "dh=#{Base64.urlsafe_encode64(payload[:server_public_key_bn]).delete('=')}"
36
37
  }
38
+ header["Authorization"] = "key=#{api_key}" unless api_key.empty?
37
39
  req = Net::HTTP::Post.new(uri.request_uri, header)
38
40
  req.body = payload[:ciphertext]
39
41
  res = http.request(req)
42
+ p res.body
40
43
  return ("201" == res.code) ? true : false
41
44
  rescue
42
45
  return false
@@ -1,3 +1,3 @@
1
1
  module Webpush
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpush
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zaru@sakuraba
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-31 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.5.1
113
+ rubygems_version: 2.4.5.1
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Encryption Utilities for Web Push payload.