Class: Xiaomi::Push::Services::Message

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

Overview

单条消息类 API

允许向单个设备或多个设备发送同样的推送消息

设备的标识支持 reg_id/alias/user/topic/topics/all

Constant Summary collapse

MESSAGE_TYPE =

消息类型模板

{
  reg_id: {
    uri: 'regid',
    query: 'registration_id'
  },
  alias: {
    uri: 'alias',
    query: 'alias'
  },
  user: {
    uri: 'user_account',
    query: 'user_account'
  },
  topic: {
    uri: 'topic',
    query: 'topic'
  },
  topics: {
    uri: 'multi_topic',
    query: 'topics'
  },
  topic_op: {
    uri: 'multi_topic',
    query: 'topic_op'
  },
  all: {
    uri: 'all',
    query: 'all'
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Message

Returns a new instance of Message.



50
51
52
# File 'lib/xiaomi/push/services/message.rb', line 50

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/message.rb', line 15

def context
  @context
end

Instance Method Details

#counters(start_date, end_date, package_name) ⇒ Hash

获取消息的统计数据

Examples:

获取 2017-09-01 到 2017-09-30 应用 com.icyleaf.app.helloworld 统计数据

counters('20170901', '20170930', 'com.icyleaf.app.helloworld')

Parameters:

  • start_date (String)

    开始日期,格式 yyyyMMdd

  • end_date (String)

    结束日期,必须小于 30 天。格式 yyyyMMdd

  • package_name (String)

    包名,Android 为 package name,iOS 为 Bundle identifier

Returns:

  • (Hash)

    小米返回数据结构

See Also:



92
93
94
95
96
97
98
99
100
101
# File 'lib/xiaomi/push/services/message.rb', line 92

def counters(start_date, end_date, package_name)
  url = @context.build_uri('stats/message/counters')
  params = {
    start_date: start_date,
    end_date: end_date,
    restricted_package_name: package_name
  }

  @context.get(url, params)
end

#send(**options) ⇒ Hash

推送消息

Parameters:

Returns:

  • (Hash)

    小米返回数据结构

Raises:

  • (RequestError)

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

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/xiaomi/push/services/message.rb', line 63

def send(**options)
  type, value = fetch_message_type(options)
  if type && value
    url = @context.build_uri("message/#{type[:uri]}")
    if options[:message].kind_of?Xiaomi::Push::Message::Base
      options[:message].type(type[:query], value)
      params = options[:message].to_params
    else
      params = options[:message]
      params[type[:query].to_sym] = value
    end

    @context.post(url, params)
  else
    raise Xiaomi::Push::RequestError, '无效的消息类型,请检查是否符合这些类型: reg_id/alias/topic/topics/all'
  end
end