urbanairship / ruby-library

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

Can't not push notification to ios with params badge on v3 #93

Closed andyduong1920 closed 8 years ago

andyduong1920 commented 8 years ago

Hey, I'm using Urban AirShip via https://github.com/urbanairship/ruby-library. I try to send push notification to ios with param Urbanairship.ios(alert: "hello", badge: "1") But I get error

    D, [2016-03-10 11:32:02#8355] DEBUG -- Urbanairship: Making POST request to https://go.urbanairship.com/api/push/.
        Headers:
            content-type: application/json
            accept: application/vnd.urbanairship+json; version=3
        Body:
    {"audience":{"device_token":"87F8FA42DC250666E5D3B82C2006C1A9BFA550F069FD24807587FF72C80BFB4A"},"notification":{"alert":"hello","badge":"1"},"device_types":"all"}
    D, [2016-03-10 11:32:03#8355] DEBUG -- Urbanairship: Received 400 response. Headers:
        {:content_type=>"application/vnd.urbanairship+json; version=3", :content_length=>"257", :date=>"Thu, 10 Mar 2016 04:32:03 GMT", :connection=>"close"}
    Body:
        {"ok"=>false, "error"=>"Could not parse request body.", "error_code"=>40000, "details"=>{"error"=>"The key 'badge' is not allowed in this context", "path"=>"notification.badge", "location"=>{"line"=>1, "column"=>138}}, "operation_id"=>"e687b7d9-5d6d-46e5-a9c3-b5332b7f3425"}
    E, [2016-03-10 11:32:03#8355] ERROR -- Urbanairship: Request failed with status 400: '40000 Could not parse request body.': {"ok"=>false, "error"=>"Could not parse request body.", "error_code"=>40000, "details"=>{"error"=>"The key 'badge' is not allowed in this context", "path"=>"notification.badge", "location"=>{"line"=>1, "column"=>138}}, "operation_id"=>"e687b7d9-5d6d-46e5-a9c3-b5332b7f3425"}

However, I try with Urbanairship.ios(alert: "hello") or Urbanairship.notification(alert: "hello") it work OK.

Please help me, thank you.

Bill-SnowWolf commented 8 years ago

Hi @anduonghien , is your issue resolved

madelgi commented 8 years ago

Hi @Bill-SnowWolf -- when passing a value to badge, you must precede it with either a plus or minus, e.g.:

Urbanairship.ios(alert: "hello", badge: "+1")
Urbanairship.ios(alert: "hello", badge: "-1")

The "+1" will increase a user's badge count by one, while the "-1" will decrease the count by one. Please let me know if this addresses your issue.

EDIT: I should also mention that passing in an integer will reset a user's badge count to the specified value. While the above examples increase or decrease the badge value by one, the following payload would reset a user's badge count to 23:

Urbanairship.ios(alert: "hello", badge: 23)
Bill-SnowWolf commented 8 years ago

@madelgi There seems like a problem with Urbanairship API V3. Even I tried to manually send curl request with badge parameter, it will fail with this error:

{"ok":false,"error":"Could not parse request body.","error_code":40000,"details":{"error":"The key 'badge' is not allowed in this context","path":"notification.badge","location":{"line":1,"column":117}},"operation_id":"f54ed775-811e-4fe2-bcdf-ad5e0a3c2654"}
Bill-SnowWolf commented 8 years ago

So for future reference, the platform specific parameters should be wrapped in a platform object. The documentation has a typo.

Instead of calling:

push.notification = UA.ios(alert: "hello", badge: "+1")

it should be:

push.notification = UA.notification(ios: UA.ios(alert: "Hello iOS", badge: "+1"))
madelgi commented 8 years ago

@Bill-SnowWolf Thank you for bringing this to our attention! The docs have been updated with valid notification payloads.