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

Abr*_*odj 7 iphone ruby-on-rails push-notification urbanairship.com ios

使用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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

Moh*_*ain 23

更好的方法是使用UrbanAirship Groupon版本.他们的文档非常清楚地指出了每一件事,而且代码更加简洁.在我的应用程序中进行工作和测试.

从阅读我的文件,(以及更多的评论和所有): -

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

  gem install system_timer
Run Code Online (Sandbox Code Playgroud)

安装

 gem install urbanairship 
 # or specify in your gem file 
 gem "urbanairship"
Run Code Online (Sandbox Code Playgroud)

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

    Urbanairship.application_key = 'application-key'
    Urbanairship.application_secret = 'application-secret'
    Urbanairship.master_secret = 'master-secret'
    Urbanairship.logger = Rails.logger
    Urbanairship.request_timeout = 5 # default
Run Code Online (Sandbox Code Playgroud)

用法

注册设备令牌

    Urbanairship.register_device 'DEVICE-TOKEN' # => true
Run Code Online (Sandbox Code Playgroud)

取消注册设备令牌

    Urbanairship.unregister_device 'DEVICE-TOKEN' # => true
Run Code Online (Sandbox Code Playgroud)

发送推送通知(用于即时交付删除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
Run Code Online (Sandbox Code Playgroud)

批量推送通知发送(用于即时交付删除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
Run Code Online (Sandbox Code Playgroud)

发送广播通知Urbanairship允许您向应用的所有活动注册设备令牌发送广播通知.(对于即时投递,删除schedule_for属性)

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

    Urbanairship.broadcast_push notification # => true
Run Code Online (Sandbox Code Playgroud)