Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ require 'huawei_push_kit'
device_token = "<huawei_device_token>"
client = HuaweiPushKit::Client.new

# Send notification to a single Huawei device with custom payload
client.send_push(
{
'validate_only' => false,
'message' =>
{
'android' => {
'notification' => {
'title' => 'message title',
'body' => 'message body'
'click_action' => { 'type' => 3 },
'badge' => {
'add_num' => 1,
'class' => 'com.huawei.codelabpush.MainActivity',
'set_num' => 10
}
},
'ttl' => '1000'
},
'token' => [device_token]
}
}
)

# Send notification to a single Huawei device
client.send_push_notification(device_token, "Title", "Message")

Expand Down
2 changes: 1 addition & 1 deletion lib/huawei_push_kit/api_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def error
end

def body
JSON.parse(response.body)
response
end

private
Expand Down
60 changes: 35 additions & 25 deletions lib/huawei_push_kit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ class Client
def initialize(client_id = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_ID"), client_secret = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_SECRET"))
@client_id = client_id
@client_secret = client_secret

fetch_access_token
end

def send_push_notification(device_token, title, body, type = nil, badge = nil, post_id = nil, comment_id = nil,
chat_id = nil, avatar = nil)
def send_push(payload)
response = push_api_connection.post("/v1/#{client_id}/messages:send") do |req|
req.body = build_notification_body(device_token, title, body, type, badge, post_id, comment_id, chat_id, avatar)
req.body = payload.to_json
end

ApiResponse.new(response)
end

def send_push_notification(device_token, title, body, type = nil, badge = nil, post_id = nil, comment_id = nil,
chat_id = nil, avatar = nil)

send_push(build_notification_body(device_token, title, body, type, badge, post_id, comment_id, chat_id, avatar))
end

def send_push_notification_to_topic(topic_name, title, body, type = nil, badge = nil, post_id = nil,
comment_id = nil, chat_id = nil, avatar = nil)
response = push_api_connection.post("/v1/#{client_id}/messages:send") do |req|
req.body = build_topic_notification_body(topic_name, title, body, type, badge, post_id, comment_id, chat_id,
avatar)
end

ApiResponse.new(response)
send_push(build_topic_notification_body(topic_name, title, body, type, badge, post_id, comment_id, chat_id, avatar))
end

def subscribe_to_topic(topic_name, device_tokens)
Expand Down Expand Up @@ -60,7 +63,24 @@ def topic_list(device_token)

private

attr_reader :client_id, :client_secret
attr_reader :client_id, :client_secret, :access_token, :access_token_expiry

def fetch_access_token
return if access_token_expiry && access_token_expiry > Time.now.utc + 300

response = oauth_connection.post("/oauth2/v3/token") do |req|
req.body = URI.encode_www_form({
grant_type: "client_credentials",
client_id:,
client_secret:
})
end

body = JSON.parse(response.body)

@access_token = body['access_token']
@access_token_expiry = Time.now.utc + body['expires_in']
end

def build_topic_notification_body(topic_name, title, text, type = nil, badge = nil, post_id = nil,
comment_id = nil, chat_id = nil, avatar = nil)
Expand All @@ -76,10 +96,10 @@ def build_topic_notification_body(topic_name, title, text, type = nil, badge = n
comment_id:,
chat_id:,
avatar:
}.to_json,
},
topic: topic_name
}
}.to_json
}
end

def build_notification_body(device_token, title, text, type = nil, badge = nil, post_id = nil, comment_id = nil,
Expand All @@ -96,27 +116,17 @@ def build_notification_body(device_token, title, text, type = nil, badge = nil,
comment_id:,
chat_id:,
avatar:
}.to_json,
},
token: [
device_token
]
}
}.to_json
end

def access_token
response = oauth_connection.post("/oauth2/v3/token") do |req|
req.body = URI.encode_www_form({
grant_type: "client_credentials",
client_id:,
client_secret:
})
end

JSON.parse(response.body)["access_token"]
}
end

def push_api_connection
fetch_access_token

Faraday.new(PUSH_API_URI) do |faraday|
setup_connection(faraday, CONTENT_JSON, "Bearer #{access_token}")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/huawei_push_kit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

before do
stub_request(:post, "https://oauth-login.cloud.huawei.com/oauth2/v3/token")
.to_return(status: 200, body: { access_token: "access_token" }.to_json)
.to_return(status: 200, body: { access_token: "access_token", expires_in: 3600 }.to_json)
end

describe "#send_push_notification" do
Expand Down