Module: MqttPush

Defined in:
lib/mqtt_push.rb,
lib/mqtt_push/version.rb

Constant Summary collapse

PUSH_NOTIFY_APP_KEY =
  1. push_message_json_data 定义json数据

'6727fb0922117931bb65a48ee27dc0d9'
VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#push_notifyObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mqtt_push.rb', line 11

def push_notify
  check_auth = self.respond_to?(:can_push?) ? can_push? : true
  return unless check_auth

  send_mqtt_message = Proc.new do | user |
    begin
      data = push_message_json_data
      Rails.logger.error "mqtt error: data:#{data}, user_id: #{user.id}, usernmae: #{user.username}" unless data && user
      msg = {
              timestamp: Time.now.to_i,
              ticket: user.ticket,
              payload: {
                body: {
                        title: data[:title],
                        text: data[:text],
                        after_open: data[:after_open],
                        after_open_page: data[:after_open_page]
                      },
                extra: data[:extra],
                display_type: data[:display_type] || 'notification',
                play_sound: data[:play_sound] ||'true'
              }
            }
      # title, text, after_open, after_open_page为加密字段
      sign_json = msg[:payload][:body].merge!(timestamp: msg[:timestamp])
      msg.merge!(sign: sign(sign_json))
      $mqtt.publish(topic(user.id), msg.to_json)
      Rails.logger.info "mqtt push message: topic:#{topic(user.id)}, msg:#{msg.to_json}"
    rescue StandardError => e
      Rails.logger.error "mqtt push message error: #{e}"
    end
  end
  send_mqtt_message.call(user)
end