expo-community / expo-server-sdk-ruby

A Ruby library for sending push notifications with Expo's notification service
MIT License
95 stars 53 forks source link

Support for new api #1

Closed jkeam closed 7 years ago

jkeam commented 7 years ago

In the docs there seems to be a better endpoint that allows an array of messages and a more complex message type that includes things like badges. Are there plans to update this gem to support that?

maier-stefan commented 7 years ago

@jkeam thanks for impl v2. @jesseruder - Is this repo not anymore official supported? Last update was in 2016 ... and what can we do to keep this up to date?

lucidtheory commented 7 years ago

I am still having trouble with pushing groups of messages:


      messages = []

      new_users.each do |id|
        user = User.find(id)
        @chat.users << user

        if id != current_user.id && token = user.token
          message = "You have been invited to the group #{@chat.title}! Woo!"
          messages.push({
            to: token,
            sound: 'default',
            body: message
          })
        end
      end

      message_arrays = messages.each_slice(100).to_a

      message_arrays.each do |notifications|
        exponent.publish notifications
      end

I ran bundle update, does that give me the latest version of the gem from the pull request? I dont understand how to fix this

right now it only works with single notifications

jkeam commented 7 years ago

No, the latest version of the gem has not been published yet. You can see here:

https://rubygems.org/gems/exponent-server-sdk

The latest gem is from 2016. 0.0.2 - June 20, 2016 (6.5 KB)

As a result, you need to modify your Gemfile to point to this repo's master branch. You can see how to do that here:

http://bundler.io/v1.12/git.html

Let me know if this still doesn't work for you.

lucidtheory commented 7 years ago

I got it to hit this new version but for some reason the publish is only working if there is one element in the array of messages. If there are multiple it is not working. I have it set up with the code structure above.

UPDATE: Im not sure if this matters, but in the rails console when I do array.to_json it puts out with a bunch of " and \ like so: "[{\"to\":\"token\",\"sound\":\"default\",\"body\":\"message\"},{\"to\":\"token\",\"sound\":\"default\",\"body\":\"message\"}]"

If it is array.as_json it prints out cleaner: [{"to"=>"token", "sound"=>"default", "body"=>"message"}, {"to"=>"token", "sound"=>"default", "body"=>"message"}]

In the updated gem file we are calling .to_json on the messages array before calling the API. Do you think this might be the problem?

UPDATE: It is working on iPhone but not working on Android unless there is only one message in the array

jkeam commented 7 years ago

Ah ok. I'll take a look later today/tonight. PRs welcome if you beat me to it though :) On Mon, Aug 21, 2017 at 8:51 AM EsdrasEtrenne notifications@github.com wrote:

I got it to hit this new version but for some reason the publish is only working if there is one element in the array of messages. If there are multiple it is not working

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/expo/exponent-server-sdk-ruby/issues/1#issuecomment-323736507, or mute the thread https://github.com/notifications/unsubscribe-auth/AAC-2IOTEsaVMCzOryYKkvJWjorCaSmkks5saX0zgaJpZM4NJx1C .

lucidtheory commented 7 years ago

hmm. so I tried changing it to as_json but the api would not accept it. I guess it needs to be to_json.

It's weird tho, this is working for me on iOS with batching notifications but it does not work on Android unless there is only one item in the messages array. It works on iOS if there are multiple or just one.

When logging all incoming notifications, the notification is not even being received by the Android phone unless it is the only message in the messages array. Again, iPhone is working fine tho.

I guess I can send out the messages individually...I'm concerned that could really slow down the app depending on how many messages are being sent out at once though. I really like that we are able to batch them in groups of 100.

UPDATE:

woo! its fixed

I didnt make any changes to the gem, I just changed the way I was building out the messages: Correct:

    messages.push({
              'to': token,
              'sound': 'default',
              'body': message
            })

vs. Incorrect:

  messages.push({
            to: token,
            sound: 'default',
            body: message
          })

I am not sure that is the reason it works, but that is the only thing I changed and it is working.

jkeam commented 7 years ago

I tested this just now and everything seems to work as expected.

I'm a little confused that it works on ios and not android. My confusion is compounded by the fact that this gem runs on neither device. This gem would run in the server (in say rails or something).

All this gem does is deliver the data to expo's servers and then expo handles the rest of the magic to get it from their servers to the various devices.

To narrow the problem down, I would have your rails app output the payload that it's getting from both ios and android. If the payload is identical in both cases, you can be pretty sure that the error is happening further downstream.

If the payloads are not identical, then go into your client code and see if you can make the payloads the same.

jkeam commented 7 years ago

You can also test this outside of the scope of this gem. Use Postman or similar and just issue a POST to

https://exp.host/--/api/v2/push/send

with the following headers: accept: application/json accept-encoding: gzip, deflate content-type: application/json

where the payload looks like:

[{
  "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
  "body": "Hello world!"
}, {
  "to": "ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]",
  "body": "You've got mail"
}]

Make sure to use your problematic android tokens.

lucidtheory commented 7 years ago

yea i updated the post this is resolved now. I'm not sure why my solution above works but it does. The other way just isnt working on android.

jkeam commented 7 years ago

ah perfect! glad it worked for you. i'm going to mark this as closed.