Easily construct JSON payloads for Apple's push notification service.
MIT License
PushBuilder was born with one mission only: construct JSON payloads for Apple's push notification service.
PushBuilder.build(alert: 'Hello World!', badge: 3, sound: 'default').to_json
# => {"aps":{"badge":3,"alert":"Hello World!","sound":"default"}}
# Specifying custom data:
payload = PushBuilder.build(alert: 'Hello World!')
payload.custom_data[:notification_id] = 1234
payload.to_json
# => {"notification_id":1234,"aps":{"alert":"Hello World!"}}
# Specifying third party data (such as UrbanAirship aliases):
payload = PushBuilder.build(alert: 'Hello World!')
payload.third_party_data[:aliases] = %w[ 123 456 789 ]
payload.to_json
# => {"aliases":["123","456","789"],"aps":{"alert":"Hello World!"}}
# Auto crops alerts to not exceed max payload size of 256 bytes:
PushBuilder.build(alert: 'Hello World ' * 100, badge: 3, sound: 'default').to_json
# => {"aps":{"badge":3,"alert":"Hello World [...] H…","sound":"default"}}
The alert
key of the aps
dictionary only supports strings at the moment.
Technically, the alert can be customized further as described in PAYLOAD.md.
MIT License. Copyright 2013 Philipe Fatio