urbanairship / ruby-library

A Ruby wrapper for the Urban Airship API.
Other
200 stars 117 forks source link

How to provide custom payload for APNS notification? #84

Closed paneq closed 9 years ago

paneq commented 9 years ago

In version 2 of the gem it was possible to quite easily provide custom payload for APNS notification such as for example path and tenant_id as in this example:

{
  aps: {
    alert: "#{Time.now} - wake up simon"
  },
  path: "myappios://alarm/50645",
  tenant_id: 3,
}

What's the way to do it in v3 ?

I tried

push.notification = {alert: "#{Time.now} - wake up simon", path: "myappios://alarm/50645", tenant_id: 3}

I get an exception Urbanairship::Common::AirshipFailure: Urbanairship::Common::AirshipFailure with details:

{"error"=>"The key 'path' is not allowed in this context", "path"=>"notification.path", "location"=>{"line"=>1, "column"=>178}}

I tried to use extra parameter but with no luck as well and the same error.

jkvoorhis commented 9 years ago

Hey @paneq,

In v3 the formatting requires nesting platform specific portions of the notification payload like so:

push.notification = UA.notification(
    ios: {
        alert: "#{Time.now} - wake up simon",
        extra: { 
            path: "myappios://alarm/50645", 
            tenant_id: 3
        }
    }
)

You will also need to explicitly declare the platforms you are pushing to under the device_types portion of the payload:

push.device_types = UA.device_types(['ios'])

For further information on platform specific payloads please refer to our documentation on it, and let us know if you continue to experience issues.

paneq commented 9 years ago

@jkvoorhis

Like I said in last sentence of my issue description. I already tried that and it didn't work as well. I get Urbanairship::Common::AirshipFailure with given details :

=> {"error"=>"The key 'extra' is not allowed in this context", "path"=>"notification.extra", "location"=>{"line"=>1, "column"=>179}}

Here is the code:

UA = Urbanairship
cl = UA::Client.new(key:'REALKEYHERE', secret:'REALSECRETHERE')
push = cl.create_push
push.audience = UA.or( UA.device_token('REALTOKENHERE') )
push.notification = {alert: "#{Time.now} - for simon", extra:{path: "myappios://alarm/50645", tenant_id: 3}}
push.device_types = UA.device_types(['ios'])
push.send_push
paneq commented 9 years ago

@jkvoorhis I didn't notice the part you wrote about nesting. Will try that now.

paneq commented 9 years ago

@jkvoorhis Thanks, it worked.

push.notification = {ios: {alert: "#{Time.now} - for simon", extra:{path: "myappios://alarm/50645", tenant_id: 3}}}
SoCohesive commented 5 years ago

@jkvoorhis when the iOS app receives this type of push is the dictionary "extra" inside the aps dictionary?

aps: { alert: "", extra: { path: .. etc

} }

sarahdactyl71 commented 5 years ago

@SoCohesive I will take a look at this and get back to you as soon as I can.

SoCohesive commented 5 years ago

Hi!

Have you had a chance to look into this? I would just like to know what the "extra" dictionary looks like when the iOS client receives it via a push notification in the "aps" payload

On Wed, Jun 12, 2019 at 10:56 AM Sarah Kirk notifications@github.com wrote:

@SoCohesive https://github.com/SoCohesive I will take a look at this and get back to you as soon as I can.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/urbanairship/ruby-library/issues/84?email_source=notifications&email_token=AA5QHPBXJTHQCAP43LXQAGLP2E2GFA5CNFSM4BPO4KZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXRI6DA#issuecomment-501387020, or mute the thread https://github.com/notifications/unsubscribe-auth/AA5QHPBFDF2JGQI4M4GFCYTP2E2GFANCNFSM4BPO4KZA .

-- Sonam Dhingra Portfolio: http://bit.ly/SoCohesive http://bit.ly/SoCohesive LinkedIn: http://bit.ly/SonamsLinkedIn http://bit.ly/SonamsLinkedIn Follow Me: @sdhingra89

sarahdactyl71 commented 5 years ago

Hi @SoCohesive the full payload is as follows:

push.notification = UA.notification(
    ios: {
        alert: "#{Time.now} - wake up simon",
        extra: { 
            path: "myappios://alarm/50645", 
            tenant_id: 3
        }
    }
)

Furthermore, if you would want to add more than one key value pair to the payload it would look like this:


push.notification = UA.notification(
    ios: {
        alert: "#{Time.now} - wake up simon",
        extra: {
            extra: { 'key' => 'value', 'key2' => 'value2' }
        }
    }
)
SoCohesive commented 5 years ago

Hi Sarah,

Thanks for the follow up, I'm still a bit confused.

How do I set up a test push notification via the dashboard that includes an "extra" dictionary with multiple key/value pairs?

On Mon, Jun 17, 2019 at 10:28 AM Sarah Kirk notifications@github.com wrote:

Hi @SoCohesive https://github.com/SoCohesive the full payload is as follows:

push.notification = UA.notification( ios: { alert: "#{Time.now} - wake up simon", extra: { path: "myappios://alarm/50645", tenant_id: 3 } } )

Furthermore, if you would want to add more than one key value pair to the payload it would look like this:

push.notification = UA.notification( ios: { alert: "#{Time.now} - wake up simon", extra: { extra: { 'key' => 'value', 'key2' => 'value2' } } } )

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/urbanairship/ruby-library/issues/84?email_source=notifications&email_token=AA5QHPFENWSMQCR57IMZRDLP27CT7A5CNFSM4BPO4KZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX34LZQ#issuecomment-502777318, or mute the thread https://github.com/notifications/unsubscribe-auth/AA5QHPBPQRWZULB6P44TZCLP27CT7ANCNFSM4BPO4KZA .

-- Sonam Dhingra Portfolio: http://bit.ly/SoCohesive http://bit.ly/SoCohesive LinkedIn: http://bit.ly/SonamsLinkedIn http://bit.ly/SonamsLinkedIn Follow Me: @sdhingra89

sarahdactyl71 commented 5 years ago

@SoCohesive

The test push form for iOS doesn't support multiple extras. However, our regular composer does if you are on a billing plan that gives you access to it. The other option is making requests directly with the ruby library.

What may work is manually editing the payload that you would like to send through to the API on the Test Push Notification Dashboard.

For example, if you submit everything in the form for IOS the payload that is automatically produced in the Payload box looks like this:

{
    "audience": {
        "device_token": "<device_token>",
        "ios_channel": "<channel_id>"
    },
    "notification": {
        "alert": "Hello iOS",
        "ios": {
            "sound": "beep-boop",
            "badge": 123
        }
    },
    "device_types": [
        "ios"
    ]
}

From there, you may get into the text box and manually update from there to customize your POST request. For example:

{
    "audience": {
        "device_token": "<device_token>",
        "ios_channel": "<channel_id>"
    },
    "notification": {
        "alert": "Hello iOS",
        "ios": {
            "sound": "beep-boop",
            "extra": { 'key' => 'value', 'key2' => 'value2' },
            "category": 'category_name',
            "badge": 123
        }
    },
    "device_types": [
        "ios"
    ]
}