Coder Social home page Coder Social logo

apns's Introduction

APNS

a gem for the Apple Push Notification Service.

Install

sudo gem install apns

Setup:

Convert your certificate

In Keychain access export your certificate as a p12. Then run the following command to convert it to a .pem

  
    openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
  

After you have your .pem file. Set what host, port, certificate file location on the APNS class:

  
    APNS.host = 'gateway.push.apple.com' 
    # gateway.sandbox.push.apple.com is default

    APNS.pem  = '/path/to/pem/file'
    # this is the file you just created
    
    APNS.port = 2195 
    # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
  

Example (Single notification):

Then to send a push notification you can either just send a string as the alert or give it a hash for the alert, badge and sound.

  
    device_token = '123abc456def'

    APNS.send_notification(device_token, 'Hello iPhone!' )

    APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
  

Example (Multiple notifications):

You can also send multiple notifications using the same connection to Apple:

  
    device_token = '123abc456def'

    n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )

    n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
    
    APNS.send_notifications([n1, n2])
  

Send other info along with aps

You can send other application specific information as well.

  
    APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
                                         :other => {:sent => 'with apns gem'})
  

This will add the other hash to the same level as the aps hash:

  
    {"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}
  

Getting your iPhone’s device token

After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.

ApplicationAppDelegate.m

  
    - (void)applicationDidFinishLaunching:(UIApplication *)application 
    {    
        // Register with apple that this app will use push notification
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | 
          UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];

        // Your app startup logic...
        return YES;
    }

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    {
        // Convert the binary data token into an NSString (see below for the implementation of this function)
        NSString *deviceTokenAsString = stringFromDeviceTokenData(deviceToken);

        // Show the device token obtained from apple to the log
        NSLog(@"deviceToken: %@", deviceTokenAsString);
    }
  

stringFromDeviceTokenData function

This snippet comes from this stackoverflow post’s anwser.


NSString* stringFromDeviceTokenData(NSData *deviceToken) { const char *data = [deviceToken bytes]; NSMutableString* token = [NSMutableString string];

for (int i = 0; i < [deviceToken length]; i++) { [token appendFormat:@"%02.2hhX", data[i]]; } return [[token copy] autorelease]; }

For more information on Apple Push Notifications you can see Apple Developer Documentation here.

apns's People

Contributors

jpoz avatar michaelbaker avatar mvanholstyn avatar samchandra avatar shuhei avatar toto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.