Class: HuaweiPushKit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/huawei_push_kit/client.rb

Constant Summary collapse

PUSH_API_URI =
"https://push-api.cloud.huawei.com"
OAUTH_URI =
"https://oauth-login.cloud.huawei.com"
CONTENT_JSON =
"application/json"
CONTENT_URL_ENCODED =
"application/x-www-form-urlencoded"

Instance Method Summary collapse

Constructor Details

#initialize(client_id = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_ID"), client_secret = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_SECRET")) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/huawei_push_kit/client.rb', line 13

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
end

Instance Method Details

#send_push_notification(device_token, title, body, type = nil, badge = nil, post_id = nil, comment_id = nil, chat_id = nil, avatar = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/huawei_push_kit/client.rb', line 18

def send_push_notification(device_token, 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_notification_body(device_token, title, body, type, badge, post_id, comment_id, chat_id, avatar)
  end

  ApiResponse.new(response)
end

#send_push_notification_to_topic(topic_name, title, body, type = nil, badge = nil, post_id = nil, comment_id = nil, chat_id = nil, avatar = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/huawei_push_kit/client.rb', line 27

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)
end

#subscribe_to_topic(topic_name, device_tokens) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/huawei_push_kit/client.rb', line 37

def subscribe_to_topic(topic_name, device_tokens)
  response = push_api_connection.post("/v1/#{client_id}/topic:subscribe") do |req|
    req.body = { topic: topic_name, tokenArray: Array(device_tokens) }.to_json
  end

  ApiResponse.new(response)
end

#topic_list(device_token) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/huawei_push_kit/client.rb', line 53

def topic_list(device_token)
  response = push_api_connection.post("/v1/#{client_id}/topic:list") do |req|
    req.body = { token: device_token }.to_json
  end

  ApiResponse.new(response)
end

#unsubscribe_from_topic(topic_name, device_tokens) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/huawei_push_kit/client.rb', line 45

def unsubscribe_from_topic(topic_name, device_tokens)
  response = push_api_connection.post("/v1/#{client_id}/topic:unsubscribe") do |req|
    req.body = { topic: topic_name, tokenArray: Array(device_tokens) }.to_json
  end

  ApiResponse.new(response)
end