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

发布于 2024-11-19 02:22:11 字数 514 浏览 3 评论 0原文

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

来自城市飞艇文档:

HTTP POST 到 /api/push/broadcast/ 将给定的通知发送给所有人 注册的设备令牌 应用。有效负载采用 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"
    ]
}

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

What is the best way to send push notifications through Urban AirShip using a Rails 3 web app?

From Urban Airship documentation:

An HTTP POST to /api/push/broadcast/
sends the given notification to all
registered device tokens for the
application. The payload is in JSON
with content-type application/json,
with this structure:

{
    "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"
    ]
}

I really don't know where to start!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月如刀 2024-11-26 02:22:11

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

从自述文件中,(还有一些更多的注释和全部):-

注意:如果您使用 Ruby 1.8,您还应该安装 system_timer gem 以获得更可靠的超时行为。请参阅 http://ph7spot.com/musings/system-timer 了解更多信息。基本上

  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

Better way is to use UrbanAirship Groupon version. Their docs specify every thing very clearly and it will much neater code. Worked and tested in my application.

From the read me file, (with some more comments and all):-

Note: if you are using Ruby 1.8, you should also install the system_timer gem for more reliable timeout behaviour. See http://ph7spot.com/musings/system-timer for more information. Baically

  gem install system_timer

Installation

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

Configuration define all this in initializes directory and then make sure u restart your application.

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

Usage

Registering a device token

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

Unregistering a device token

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

Sending a push notification (for instant delivery remove schedule_for attribute)

    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

Batching push notification sends (for instant delivery remove schedule_for attribute)

    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

Sending broadcast notifications
Urbanairship allows you to send a broadcast notification to all active registered device tokens for your app.(for instant delivery remove schedule_for attribute)

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

    Urbanairship.broadcast_push notification # => true
罗罗贝儿 2024-11-26 02:22:11

好的,这是从 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"  

干杯!

Ok, this is a simple way of sending a urban airship broadcast from ROR controller:

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"  

Cheers!

韬韬不绝 2024-11-26 02:22:11

在 Rails 4.2.x 和 Ruby 2.3.x 中,我使用 'urbanairship' gem,最好参考最新的文档 这里

发送到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

In Rails 4.2.x and Ruby 2.3.x, I'm using the 'urbanairship' gem, it is best to reference the most recent documentation found here.

Example of sending to 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文