使用Urban AirShip和Rails 3 for iphone推送通知

时间:2011-07-08 09:28:47

标签: iphone ruby-on-rails ios push-notification urbanairship.com

使用Rails 3网络应用程序通过Urban AirShip发送推送通知的最佳方式是什么?

来自Urban Airship文档:

  

到/ api / push / broadcast /的HTTP POST   将给定的通知发送给所有人   已注册的设备令牌   应用。有效负载采用JSON格式   内容类型application / json,   这种结构:

{
    "aps": {
         "badge": 15,
         "alert": "Hello from Urban Airship!",
         "sound": "cat.caf"
    },
    "exclude_tokens": [
        "device token you want to skip",
        "another device token you want to skip"
    ]
}

我真的不知道从哪里开始!

4 个答案:

答案 0 :(得分:23)

更好的方法是使用UrbanAirship Groupon version。他们的文档非常清楚地指出了每一件事,而且代码更加简洁。在我的申请中进行了测试和测试。

从自述文件中,(以及更多评论和所有内容): -

注意:如果您使用的是Ruby 1.8,则还应安装system_timer gem以获得更可靠的超时行为。有关详细信息,请参阅http://ph7spot.com/musings/system-timer。 Baically

  gem install system_timer

<强>安装

 gem install urbanairship 
 # or specify in your gem file 
 gem "urbanairship"

配置在初始化目录中定义所有这些,然后确保重新启动应用程序。

    Urbanairship.application_key = 'application-key'
    Urbanairship.application_secret = 'application-secret'
    Urbanairship.master_secret = 'master-secret'
    Urbanairship.logger = Rails.logger
    Urbanairship.request_timeout = 5 # default

<强>用法

注册设备令牌

    Urbanairship.register_device 'DEVICE-TOKEN' # => true

取消注册设备令牌

    Urbanairship.unregister_device 'DEVICE-TOKEN' # => true

发送推送通知(用于即时交付删除schedule_for属性)

    notification = {
      :schedule_for => 1.hour.from_now,
      :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
      :aps => {:alert => 'You have a new message!', :badge => 1}
    }

    Urbanairship.push notification # => true

批处理推送通知发送(用于即时交付删除schedule_for属性)

    notifications = [
      {
        :schedule_for => 1.hour.from_now,
        :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
        :aps => {:alert => 'You have a new message!', :badge => 1}
      },
      {
        :schedule_for => 3.hours.from_now,
        :device_tokens => ['DEVICE-TOKEN-THREE'],
        :aps => {:alert => 'You have a new message!', :badge => 1}
      }
    ]

        Urbanairship.batch_push notifications # => true

发送广播通知 Urbanairship允许您向应用程序的所有活动注册设备令牌发送广播通知。(即时交付删除schedule_for属性)

    notification = {
      :schedule_for => 1.hour.from_now,
      :aps => {:alert => 'Important announcement!', :badge => 1}
    }

    Urbanairship.broadcast_push notification # => true

答案 1 :(得分:0)

答案 2 :(得分:0)

好的,这是从ROR控制器发送城市飞艇广播的简单方法:

require 'net/http'
require 'net/https'
require 'open-uri'    

app_key = 'JJqr...'
app_secret = 'lAu7...'
master_secret = 'K81P...'

payload ={
    "aps" => {"badge"  => "0", "alert" => "My own message", "sound" => ""}
}.to_json

full_path = 'https://go.urbanairship.com/api/push/broadcast/'
url = URI.parse(full_path)    
req = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json'})
req.body = payload
req.basic_auth app_key, master_secret

con = Net::HTTP.new(url.host, url.port)
con.use_ssl = true

r = con.start {|http| http.request(req)}

logger.info "\n\n##############\n\n  " + "Resonse body: " + r.body + "  \n\n##############\n\n"  

干杯!

答案 3 :(得分:0)

在Rails 4.2.x和Ruby 2.3.x中,我使用&#39; urbanairship&#39; gem,最好参考here找到的最新文档。

发送到ios的示例:

def deliver_push_notification recipient_uuid, message
  ua             = Urbanairship
  client         = ua::Client.new(key: ENV["URBAN_AIRSHIP_APP_KEY"], secret: ENV["URBAN_AIRSHIP_MASTER_SECRET"])
  p              = client.create_push
  p.audience     = ua.ios_channel(recipient_uuid)
  p.notification = ua.notification(ios: ua.ios(alert: message))
  p.device_types = ua.device_types(["ios"])
  p.send_push
end