Skip to content

Push notifications - proof of concept #30

@annaswims

Description

@annaswims

I'm not sure if I'm going to have a chance to implement this properly before I roll of my contract, but I expect someone will find this push notification proof of concept helpful. 90% of the effort was just finding the right documentation.

Presuming the apnotic gem is installed...

require 'apnotic'

# create a persistent connection
connection = Apnotic::Connection.new(cert_path: ENV.fetch('PASSKIT_PRIVATE_P12_CERTIFICATE'), cert_pass: ENV.fetch('PASSKIT_CERTIFICATE_KEY'))

# Simplest example  - get the correct token from the Device table
token = Passkit::Device.last.push_token

## Or,  more realistic token from app data:
# @generator = User.find 187
# push_tokens = @generator.passkit_passes.includes(:devices).collect{ |p| p.devices.pluck(:push_token)}.flatten
# token = push_tokens.first

## ensure that @generator has an updated created_at, so that the API returns an updated pass
# @generator.touch

# create a notification for a specific device token
notification       = Apnotic::Notification.new(token)
notification.topic = ENV.fetch('PASSKIT_PASS_TYPE_IDENTIFIER')

# send (this is a blocking call)
response = connection.push(notification)

## read the response, if you want
# response.ok?      # => true
# response.status   # => '200'
# response.headers  # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"}
# response.body     # => ""

# close the connection
connection.close

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions