calonso / rails-push-notifications

Rails iOS, Android and Windows Phone Push Notifications made easy!!
https://rubygems.org/gems/rails-push-notifications
MIT License
138 stars 44 forks source link

Mulitple Notifications for the same notification #13

Closed saademou closed 6 years ago

saademou commented 7 years ago

Thank you for your awesome gem !

When calling app.push_notifications, I get a response success and it works perfectly However, i receive the same notification several Times on iPhone ( not on android and Windows Phone )

First One => 1 seconde after sending the notification Second one => 1.5 secondes later Third one => 1.7 secondes later ... ... I am using ruby 2.2.1p85, Rails 4.1.0 please suggest what could be the issue.

calonso commented 7 years ago

Hi @saademou first of all I'm so glad that the gem is being useful for you. About the issue you mention I'm not really sure what may be happening, I've never heard of it before but I'd advise you to double check you're not generating multiple notifications, and if that's not the case then I'm afraid you'll have to debug inside the inner ruby-push-notifications gem to see if it is actually doing two submissions to the APNS service.

Anyway I'm happy to support you on whatever you may need during this troubleshooting process.

Regards

saademou commented 7 years ago

hi @calonso thanks for your answer. here the implementation of the notification part on the server side :

t = Thread.new do
        begin
        app_type = RailsPushNotifications::APNSApp.find_by(apns_app: destination[:mob_identity])
        if !app_type.blank?
           notification = app_type.notifications.create(destinations: [destination[:token]], data: { aps: { alert: data, sound: "#{sound}.wav", badge: 1 } })
            arr_result = app_type.push_notifications
            arr_result.each do |result|
                if result.success == 1
                                   Rails.logger.info "[JOBS][ApnsJob] [perform] APNS notification has been sent "
                else
                  Rails.logger.error "[JOBS][ApnsJob] [perform] APNS notification has not been sent"
                end
            end
            else
                Rails.logger.error "[JOBS][ApnsJob] [perform] APNS APP is null "
            end
        rescue Exception => e
            Rails.logger.error "#{e.message} \n".concat( e.backtrace.to_sentence(words_connector: "\n", last_word_connector: "\n") ).concat("\n\n")
            error_catch = true
        end
    end

Can you tell me what's going wrong in my code plz

calonso commented 7 years ago

TBH, that looks quite right. It is also quite a long time since I last worked with this product so I'm a bit rusty as well.

A couple of questions come to me right now.

  1. Has this always happened?
  2. Can you please check that the destinations are not repeated? I mean, the only thing I can think of right now is that the destinations array is actually repeating the tokens and that's why more than one notification is reaching the phone. It would make even more sense if the notification's contents are actually equal.
saademou commented 7 years ago

Thanks again :)

So i investigated my code, it happens only when i have more than 5 devices to notify.

In fact, looking the Notifications in the Database by doing :

RailsPushNotifications::Notification.where(success:nil, failed:nil)

it returns some x records, Is it normal ?

Does notification stay frozen until a new push_notifications

calonso commented 7 years ago

Well, that could definitely lead to some notifications being sent several times. A notification is pushed if sent = false, and that flag, as success and failed values are based on the results values as you can see here so that may mean that there is an issue with those callbacks, but on the other hand, that would happen with all notification types, not just iOS ones...

Coming back to my questions...Has this happened since the very beginning?

And yes, notifications are updated when push_notifications is invoked. Are the notifications that you can see in your db unsent expected to be unsent? Those ones may lead us somewhere...

saademou commented 7 years ago

Indeed, i am testing your gem only on iOS devices.

Has this happened since the very beginning?

1- The gem works fine when I send a single notification to the device, it receives it

2- On the other hand, when I send several notifications, some will be sent and some will remain blocked until the arrival of a new notification system. So Customers will receive old and new notifications, which creates a confusion for them by thinking that these are the same notifications

All the notifications must be sended to devices

calonso commented 7 years ago

Well... This sounds definitely as a bug. Can you please debug if the results value assigned here is nil for the notifications that remain 'blocked'? (i.e.) to_send[i].results is nil for all but the first element.

I think that would explain the behaviour you're seeing.

saademou commented 7 years ago

I will debug the results and keep u in touch.

Is it possible to send a single notification without calling push_notifications.

What i need To do is to create a notification and send it immedialty. Dont care about old pending ones

calonso commented 7 years ago

Cool, thanks for your effort!!

Yes, you can create and send the notification straight away. See this example in my rails push notifications test app

Please, let me know the outcome of your debug.

saademou commented 7 years ago

I saw and understood your examples. You use push_notifications all the time, it will push all pending notifications. I need to push only one ( single one )

calonso commented 7 years ago

Ok, got you.

So you can tell the pusher to push only one by providing only the notification you're interested in. Here you can see how it normally does to push all of them, so you can do the same but instead of for all the pending ones, just for the one you want

saademou commented 7 years ago

Thanks again :)

I found this link

https://github.com/calonso/ruby-push-notifications/blob/master/examples/apns.rb

calonso commented 7 years ago

Hi @saademou.

How are you doing with this? Did you manage to find the error?

Regards

calonso commented 6 years ago

Closing as stale... Please reopen if needed or add more info to the issue if you found anything interesting.