Skip to content

Commit

Permalink
Support for native notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
vivangkumar committed Jun 28, 2016
1 parent 4776a6d commit 51b2bc8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/pusher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class HTTPError < Error; attr_accessor :original_error; end
class << self
extend Forwardable

def_delegators :default_client, :scheme, :host, :port, :app_id, :key, :secret, :http_proxy
def_delegators :default_client, :scheme=, :host=, :port=, :app_id=, :key=, :secret=, :http_proxy=
def_delegators :default_client, :scheme, :host, :port, :app_id, :key, :secret, :http_proxy, :notification_host
def_delegators :default_client, :scheme=, :host=, :port=, :app_id=, :key=, :secret=, :http_proxy=, :notification_host=

def_delegators :default_client, :authentication_token, :url
def_delegators :default_client, :encrypted=, :url=, :cluster=
Expand All @@ -37,6 +37,7 @@ class << self
def_delegators :default_client, :get, :get_async, :post, :post_async
def_delegators :default_client, :channels, :channel_info, :channel_users, :trigger, :trigger_async
def_delegators :default_client, :authenticate, :webhook, :channel, :[]
def_delegators :default_client, :notify

attr_writer :logger

Expand All @@ -61,3 +62,4 @@ def default_client
require 'pusher/request'
require 'pusher/resource'
require 'pusher/webhook'
require 'pusher/native_notification/client'
31 changes: 27 additions & 4 deletions lib/pusher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pusher
class Client
attr_accessor :scheme, :host, :port, :app_id, :key, :secret
attr_accessor :scheme, :host, :port, :app_id, :key, :secret, :notification_host
attr_reader :http_proxy, :proxy
attr_writer :connect_timeout, :send_timeout, :receive_timeout,
:keep_alive_timeout
Expand Down Expand Up @@ -32,9 +32,14 @@ def initialize(options = {})
merged_options[:host] = "api.pusherapp.com"
end

@scheme, @host, @port, @app_id, @key, @secret = merged_options.values_at(
:scheme, :host, :port, :app_id, :key, :secret
)
# TODO: Change host name when finalized
merged_options[:notification_host] =
options.fetch(:notification_host, "hedwig-staging.herokuapp.com")

@scheme, @host, @port, @app_id, @key, @secret, @notification_host =
merged_options.values_at(
:scheme, :host, :port, :app_id, :key, :secret, :notification_host
)

@http_proxy = nil
self.http_proxy = options[:http_proxy] if options[:http_proxy]
Expand Down Expand Up @@ -298,6 +303,24 @@ def trigger_batch_async(*events)
post_async('/batch_events', trigger_batch_params(events.flatten))
end

def notification_client
@notification_client ||= NativeNotification::Client.new(@app_id, @notification_host, self)
end


# Send a push notification
#
# POST /apps/[app_id]/notifications
#
# @param interests [Array] An array of interests
# @param message [String] Message to send
# @param options [Hash] Additional platform specific options
#
# @return [Hash]
def notify(interests, data = {})
notification_client.notify(interests, data)
end

# Generate the expected response for an authentication endpoint.
# See http://pusher.com/docs/authenticating_users for details.
#
Expand Down
62 changes: 62 additions & 0 deletions lib/pusher/native_notification/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Pusher
module NativeNotification
class Client
attr_reader :app_id, :host

API_PREFIX = "customer_api"
API_VERSION = "v1"

def initialize(app_id, host, pusher_client)
@app_id = app_id
@host = host
@pusher_client = pusher_client
end

# Send a notification via the native notifications API
def notify(interests, data = {})
Request.new(
@pusher_client,
:post,
url("/notifications"),
{},
payload(interests, data)
).send_sync
end

private

# TODO: Actual links
#
# {
# interests: [Array of interests],
# apns: {
# See https://pusher.com/docs/native_notifications/payloads#apns
# },
# gcm: {
# See https://pusher.com/docs/native_notifications/payloads#gcm
# }
# }
#
# @raise [Pusher::Error] if the `apns` or `gcm` key does not exist
# @return [String]
def payload(interests, data)
interests = Array(interests).map(&:to_s)

payload = { interests: interests }

unless (data.has_key?(:apns) || data.has_key?(:gcm))
raise Pusher::Error, "GCM or APNS data must be provided"
end

payload.merge({ gcm: data[:gcm] }) if data.has_key?(:gcm)
payload.merge({ apns: data[:apns] }) if data.has_key?(:apns)

MultiJson.encode(payload)
end

def url(path = nil)
URI.parse("https://#{@host}/#{API_PREFIX}/#{API_VERSION}/apps/#{@app_id}#{path}")
end
end
end
end

0 comments on commit 51b2bc8

Please sign in to comment.