Search code examples
iosruby-on-railspush-notificationresque

push notification doesn't work in grocer


I am going to send a notification to ios devices by rails backend.
I have added grocer gem to Gemfile and then installed it in the project.

gem 'grocer'

I planed to send a notification in background mode. So I created resque job and added the grocer logic in app/jobs/notificationsender.rb like this.

def self.perform(language)
    @lang = language

    # Thread.new do
      while true

        begin
            pusher = Grocer.pusher(certificate:  "#{Rails.root}/lib/notification/cer.pem", # required
                       passphrase:  "llvc",                         # optional
                       gateway:     "gateway.sandbox.push.apple.com", # optional
                       port:        2195,                             #optional
                       retries:     3)

        feedback = Grocer.feedback( certificate:  "#{Rails.root}/lib/notification/cer.pem", # required
                                  passphrase:  "llvc",                        
                                  gateway:     "feedback.sandbox.push.apple.com", 
                                  port:        2196,                     
                                  retries:     3)

        note = Grocer::Notification.new(
          device_token: device_token,
          alert: message,
          sound: 'default',
          badge: 0,
          expiry: Time.now + 60*60,
          content_available: true,
          custom: {
            "linkname": linkname,
            "linkurl": linkurl
          }
        )

        feedback.each do |attempt|
          puts "Device #{attempt.device_token} failed at #{attempt.timestamp}"
        end

        pusher.push(note)
        rescue Exception => e
            puts e.class
            puts e
        end

        sleep(5)
      end #while
  end

I got a device token from iphone and sent it to backend.

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    _deviceToken = deviceToken;

    // Store the deviceToken in the current installation and save it to Parse.
    if (currentInstallation == nil) {
        currentInstallation = [[BSGInstallation alloc] init];
    }

    currentInstallation.delegate = self;
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

I downloaded an apple pushnotification development certificate and generated p12 from it and then created cer.pem file from command line.
I signed the ios project with app development cer for debug and distribution for release and assigned development provisioning. Of course I added device identifier to the provisioning correctly.
After building ios, backend, resque job I got a notification after every 5 seconds in iPhone. I installed the app on iPad. but the notification doesn't get on the iPad. Of course the id of iphone and ipad were correctly registered. And I confirmed the device token of iphone and ipad was passed on the Grocer::notification.new()
but the notification doesn't work in iPad. So I reset the server and reinstalled on each devices. At first I tested for iPad. the result was the same. And then I move onto iPhone. the system also didn't work in the iphone. Before it worked. It was very strange.
So I want to know the followings.
1) the reason why the notification is received by iphone or ipad. I want to know the as many branches as possible so that i can detect the reason quickly by following the steps.
2) Can I check the notification is arrived until APNS?
Thanks for reading my long description.


Solution

  • There were no errors in IOS and rails code.
    I have took a code signing in ios and created a pem file by the url.
    https://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
    I got an error when following the github tutorial. Thanks.