ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
How to build a server and a iPhone client
   application using the Apple Push
         Noti?cation Service
            2009/7/21 @ actindi
               Shu MASUDA
2
Apple Push Noti?cation Service (APNs)
What is the Apple Push Noti?cation Service?



 ? iPhone OS 3.0
 ?                          3

   ? Alert
   ? Badge
   ? Sound
 ?
3
Client Implementation Overview

 ¡ï APNs
  ?   UIApplication   registerForRemoteNoti?cationTypes:



  ?   UIApplication   application:didRegisterForRemoteNoti?cationsWithDeviceToken:
                                                         Device Token


 ¡ï APNs
  ?   UIApplicationDelegate    application:didReceiveRemoteNoti?cation:



  ?   UIApplication Delegate   applicationDidFinishLaunching:
4
Client Implementation




 ?           Xcode
5
Server Implementation Overview


 1. App ID


 2.

 3. Interface APNS


 4.          Ruby
6
Generating App ID




                       Bundle
     Identi?er   ¡±*¡±
6
Generating App ID




     Enable for Apple Push Noti?cation service
6
Generating App ID




     Enable for Apple Push Noti?cation service
7
Generating Key
 1.   p12     pem

      ?    openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in
           apns-dev-cert.p12

      ?    openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-
           key.p12
 2.   1.

      ?    openssl rsa -in apns-dev-key.pem -out apns-dev-key-
           noenc.pem
 3.

      ?    cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
 4.   OpenSSL

      ?    openssl s_client -connect gateway.sandbox.push.apple.com:
           2195 -prexit -state -cert apns-dev-cert.pem -key apns-dev-
           key-noenc.pem
8
Creating the Push Noti?cation Interface

 ? Binary format of the push noti?cation interface



 ?              (payload)   JSON
  {
      ¡°aps¡±: {
        ¡°alert¡±: ¡°You¡¯ve got a mail!¡±,
        ¡°badge¡±: 1,
        ¡°sound¡±: ¡°default¡±
      }
  }
9
Implementation in Ruby
 ¡ï Ruby

  ?   pack? unpack?? or ... ???

  ?
10
Implementation in Ruby
#! /usr/bin/env ruby
# -*- encoding: utf-8 -*-

require 'socket'
require 'openssl'

socket = TCPSocket.new('gateway.sandbox.push.apple.com', 2195)

context = OpenSSL::SSL::SSLContext.new('SSLv3')
context.cert = OpenSSL::X509::Certi?cate.new(File.read('apns-dev.pem'))
context.key = OpenSSL::PKey::RSA.new(File.read('apns-dev-key-noenc.pem'))

ssl = OpenSSL::SSL::SSLSocket.new(socket, context)
ssl.connect

device_token = ['XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX']
payload = <<-EOS
{
  "aps": {
     "alert": "#{alert}",
     "badge": 2,
     ¡°sound¡±: ¡°default¡±
  }
}
EOS

(message = []) << ['0'].pack('H') << [32].pack('n') << device_token.pack('H*') << [payload.size].pack('n') << payload
ssl.write(message.join(''))

ssl.close
socket.close
It¡¯s time to Demo!
12


¡ï
?   Miss Piggy

?   Catherine       Miss Piggy

?   Mr. Kermit      iPhone


¡ï
Miss Piggy                 Cathy (Catherine)
                                                  Miss Piggy
Miss Piggy Cathy                                                     Kermit


       Miss Piggy                   Kermit iPhone Push Noti?cation
13
References


 ?   Apple Push Noti?cation Service Programming Guide
     http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/
     RemoteNoti?cationsPG/Introduction/Introduction.html

 ?   How to build an Apple Push Noti?cation provider server (tutorial) ? Boxed Ice Blog
     http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-noti?cation-provider-
     server-tutorial/

 ?   Ruby                           - pack
     http://www.ruby-lang.org/ja/man/html/
     pack_A5C6A5F3A5D7A5ECA1BCA5C8CAB8BBFACEF3.html

 ?   SSL - 2008-01-03 -
     http://d.hatena.ne.jp/shinichiro_h/20080103#1199305204

More Related Content

How to build a server and a iPhone client application using the Apple Push Notification Service

  • 1. How to build a server and a iPhone client application using the Apple Push Noti?cation Service 2009/7/21 @ actindi Shu MASUDA
  • 2. 2 Apple Push Noti?cation Service (APNs) What is the Apple Push Noti?cation Service? ? iPhone OS 3.0 ? 3 ? Alert ? Badge ? Sound ?
  • 3. 3 Client Implementation Overview ¡ï APNs ? UIApplication registerForRemoteNoti?cationTypes: ? UIApplication application:didRegisterForRemoteNoti?cationsWithDeviceToken: Device Token ¡ï APNs ? UIApplicationDelegate application:didReceiveRemoteNoti?cation: ? UIApplication Delegate applicationDidFinishLaunching:
  • 5. 5 Server Implementation Overview 1. App ID 2. 3. Interface APNS 4. Ruby
  • 6. 6 Generating App ID Bundle Identi?er ¡±*¡±
  • 7. 6 Generating App ID Enable for Apple Push Noti?cation service
  • 8. 6 Generating App ID Enable for Apple Push Noti?cation service
  • 9. 7 Generating Key 1. p12 pem ? openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12 ? openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev- key.p12 2. 1. ? openssl rsa -in apns-dev-key.pem -out apns-dev-key- noenc.pem 3. ? cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem 4. OpenSSL ? openssl s_client -connect gateway.sandbox.push.apple.com: 2195 -prexit -state -cert apns-dev-cert.pem -key apns-dev- key-noenc.pem
  • 10. 8 Creating the Push Noti?cation Interface ? Binary format of the push noti?cation interface ? (payload) JSON { ¡°aps¡±: { ¡°alert¡±: ¡°You¡¯ve got a mail!¡±, ¡°badge¡±: 1, ¡°sound¡±: ¡°default¡± } }
  • 11. 9 Implementation in Ruby ¡ï Ruby ? pack? unpack?? or ... ??? ?
  • 12. 10 Implementation in Ruby #! /usr/bin/env ruby # -*- encoding: utf-8 -*- require 'socket' require 'openssl' socket = TCPSocket.new('gateway.sandbox.push.apple.com', 2195) context = OpenSSL::SSL::SSLContext.new('SSLv3') context.cert = OpenSSL::X509::Certi?cate.new(File.read('apns-dev.pem')) context.key = OpenSSL::PKey::RSA.new(File.read('apns-dev-key-noenc.pem')) ssl = OpenSSL::SSL::SSLSocket.new(socket, context) ssl.connect device_token = ['XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'] payload = <<-EOS { "aps": { "alert": "#{alert}", "badge": 2, ¡°sound¡±: ¡°default¡± } } EOS (message = []) << ['0'].pack('H') << [32].pack('n') << device_token.pack('H*') << [payload.size].pack('n') << payload ssl.write(message.join('')) ssl.close socket.close
  • 14. 12 ¡ï ? Miss Piggy ? Catherine Miss Piggy ? Mr. Kermit iPhone ¡ï Miss Piggy Cathy (Catherine) Miss Piggy Miss Piggy Cathy Kermit Miss Piggy Kermit iPhone Push Noti?cation
  • 15. 13 References ? Apple Push Noti?cation Service Programming Guide http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/ RemoteNoti?cationsPG/Introduction/Introduction.html ? How to build an Apple Push Noti?cation provider server (tutorial) ? Boxed Ice Blog http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-noti?cation-provider- server-tutorial/ ? Ruby - pack http://www.ruby-lang.org/ja/man/html/ pack_A5C6A5F3A5D7A5ECA1BCA5C8CAB8BBFACEF3.html ? SSL - 2008-01-03 - http://d.hatena.ne.jp/shinichiro_h/20080103#1199305204