Class: Xiaomi::Push::Services::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/xiaomi/push/services/messages.rb

Overview

消息类 API

允许向多个设备发送不同的推送消息

设备的标识支持 reg_id/alias/user_account

Constant Summary collapse

MESSAGE_TYPE =

消息类型模板

{
  reg_id: {
    uri: 'regids',
    keys: [:reg_id, :regid, :registration_id]
  },
  alias: {
    uri: 'aliases',
    keys: [:alias, :aliass, :aliases]
  },
  user: {
    uri: 'user_accounts',
    keys: [:user, :account, :useraccount, :user_account]
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Messages

Returns a new instance of Messages.



34
35
36
# File 'lib/xiaomi/push/services/messages.rb', line 34

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextClient

Returns the current value of context.

Returns:

  • (Client)

    the current value of context



15
16
17
# File 'lib/xiaomi/push/services/messages.rb', line 15

def context
  @context
end

Instance Method Details

#send(type, messages = []) ⇒ Hash

推送消息

Parameters:

  • type (Hash)

    发送消息类型,可选 :reg_id, :alias, :user

  • messages (Array) (defaults to: [])

    消息结构消息体的数组 (详见 Message::IOS, Message::Android)

Returns:

  • (Hash)

    小米返回数据结构

Raises:

  • (RequestError)

    推送消息不满足 reg_id/alias/user 会引发异常

  • (RequestError)

    消息体没有包含关键 target key 会引发异常

  • (RequestError)

    messages 不是数组存储的消息体时会引发异常

See Also:



49
50
51
52
53
# File 'lib/xiaomi/push/services/messages.rb', line 49

def send(type, messages = [])
  url = @context.build_uri("multi_messages/#{request_uri(type)}")
  params = request_params(type, messages)
  @context.post(url, params)
end