diff --git a/README.md b/README.md index 3033baf..f18b428 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,30 @@ require 'huawei_push_kit' 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") diff --git a/lib/huawei_push_kit/api_response.rb b/lib/huawei_push_kit/api_response.rb index 6a49cd0..b82cbbe 100644 --- a/lib/huawei_push_kit/api_response.rb +++ b/lib/huawei_push_kit/api_response.rb @@ -15,7 +15,7 @@ def error end def body - JSON.parse(response.body) + response end private diff --git a/lib/huawei_push_kit/client.rb b/lib/huawei_push_kit/client.rb index c16099c..de97cf1 100644 --- a/lib/huawei_push_kit/client.rb +++ b/lib/huawei_push_kit/client.rb @@ -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) @@ -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) @@ -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, @@ -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 diff --git a/spec/huawei_push_kit/client_spec.rb b/spec/huawei_push_kit/client_spec.rb index a189eac..f902acd 100644 --- a/spec/huawei_push_kit/client_spec.rb +++ b/spec/huawei_push_kit/client_spec.rb @@ -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