Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification using Urban AirShip and Rails 3 for iphone

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!

like image 506
Abramodj Avatar asked Jul 08 '11 09:07

Abramodj


People also ask

How do I set up push notifications on my iPhone?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.

Do push notifications work on iOS?

An iOS push notification is a message that pops up on an Apple device such as an iPhone. Before receiving push notifications from an app, iOS device users must explicitly give permission. Once a user opts-in, mobile app publishers can send push notifications to the users' mobile devices.


1 Answers

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
like image 163
Mohit Jain Avatar answered Sep 23 '22 16:09

Mohit Jain