ostinelli / apnotic

A Ruby APNs HTTP/2 gem able to provide instant feedback.
MIT License
480 stars 96 forks source link

Token not getting invalidated when replaced by new token #66

Closed ericmnel closed 6 years ago

ericmnel commented 6 years ago

I created an apns token for my app, and then uninstalled and reinstalled my app to get a 2nd token. I am getting 200 return codes for both tokens, when I was expecting a 400 for one of them.

I'm sure its something I am doing wrong, any help appreciated. The notification is being sent by a resque scheduler task call to a model method.


module ApplePush

  @queue = :apple_push

  def self.perform()
    ApnsNotification.where(:sent_time => nil).each do |notification|
      notification.send_to_apple
    end
  end

end

require 'apnotic'

class ApnsNotification < ApplicationRecord

  APNOTIC_POOL = Apnotic::ConnectionPool.new({
    cert_path: Rails.root.join("config", "certs", "MyCert.p12"),
    cert_pass: "mypassword"
  }, size: 5)

   def send_to_apple
       Resque.logger.info "   sending message: '" + self.message + "' to " + self.token

       stripped_token = self.token.delete(' <>')

       Resque.logger.info "   stripped token: " + stripped_token

       APNOTIC_POOL.with do |connection|

         notification          = Apnotic::Notification.new(stripped_token)
         notification.alert    = self.message
         notification.topic    = "mybundle"

         begin
            response = connection.push(notification)
         rescue Exception => e
            Resque.logger.info "        exception: " + e.message
         end

         Resque.logger.info response.to_json

         if response.status == '410' ||
            (response.status == '400' && (response.body['reason'] == 'BadDeviceToken'))

            Apns.where(:token=>self.token).each do |apns|
               Resque.logger.info "   destroying old apns: " + apns.device_id + " " + apns.token
               apns.destroy
            end
         end

         self.sent_time = Time.now.to_i
         self.save

       end
   end

end

I, [2018-05-17T01:48:50.109327 #6351]  INFO -- :    sending message: 'Eric desk unit: Lost communication' to <a37a55f4 11c7ec74 8aa35d53 08f1ff39 781b1ae1 fbda8db5 11dd9933 7ca16a46>
I, [2018-05-17T01:48:50.109515 #6351]  INFO -- :    stripped token: a37a55f411c7ec748aa35d5308f1ff39781b1ae1fbda8db511dd99337ca16a46
I, [2018-05-17T01:48:50.533902 #6351]  INFO -- : {"headers":{":status":"200","apns-id":"14965060-43c3-4585-b443-21342ad93ca9"},"body":""}
I, [2018-05-17T01:48:50.539204 #6351]  INFO -- :    sending message: 'Eric desk unit: Lost communication' to <1d867019 f5d67a87 ec876092 b52af730 49771704 7a93ff57 48cb1619 56a4dc18>
I, [2018-05-17T01:48:50.539322 #6351]  INFO -- :    stripped token: 1d867019f5d67a87ec876092b52af730497717047a93ff5748cb161956a4dc18
I, [2018-05-17T01:48:50.609411 #6351]  INFO -- : {"headers":{":status":"200","apns-id":"99457b80-0876-4bcb-a546-946fdbaefff2"},"body":""}
ericmnel commented 6 years ago

https://forums.developer.apple.com/thread/25914

Its possible that TestFlight builds don't work with the Apple feedback service. I'm starting to think that this is the issue since the feedback on Houston didn't remove old tokens either.