La parte peggiore della configurazione del servizio di notifica push è il provisioning. Il principale ostacolo che ho riscontrato è che c'è un certificato e una chiave nel file .cer scaricato dal sito di Apple, ho scritto un servizio di sistema in C# che ha inviato notifiche e le connessioni continuavano a mancare perché avevo esportato il certificato e non la chiave.
Non ricordo chi ha scritto in origine questo, ecco un po 'di codice in Python che mi ha aiutato quando stavo testando il servizio di notifica. Mi piace perché è molto semplice e funziona bene durante i test.
import socket, ssl, json, struct
# device token returned when the iPhone application
# registers to receive alerts
deviceToken = 'XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX'
thePayLoad = {
'aps': {
'alert':'Oh no! Server\'s Down!',
'sound':'k1DiveAlarm.caf',
'badge':42,
},
'test_data': { 'foo': 'bar' },
}
# Certificate issued by apple and converted to .pem format with openSSL
# Per Apple's Push Notification Guide (end of chapter 3), first export the cert in p12 format
# openssl pkcs12 -in cert.p12 -out cert.pem -nodes
# when prompted "Enter Import Password:" hit return
#
theCertfile = 'cert.pem'
#
theHost = ('gateway.sandbox.push.apple.com', 2195)
#
data = json.dumps(thePayLoad)
# Clear out spaces in the device token and convert to hex
deviceToken = deviceToken.replace(' ','')
byteToken = bytes.fromhex(deviceToken) # Python 3
# byteToken = deviceToken.decode('hex') # Python 2
theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack(theFormat, 0, 32, byteToken, len(data), data)
# Create our connection using the certfile saved locally
ssl_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM), certfile = theCertfile)
ssl_sock.connect(theHost)
# Write out our data
ssl_sock.write(theNotification)
# Close the connection -- apple would prefer that we keep
# a connection open and push data as needed.
ssl_sock.close()
C'è anche una gemma rotaie chiamato apn_on_rails che sembra funzionare abbastanza bene se si sta sviluppando un'applicazione Rails, ho appena visto oggi ed è stato in grado di inviare le notifiche dalla console.
Sul lato iPhone avrete solo bisogno di chiamare il seguente per registrare per tutti i tipi di notifiche:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
Per ricevere il token dispositivo è necessario implementare i seguenti metodi delegato:
Durante il test, è possibile avviare il dispositivo con il dispositivo Talk in console con NSLog, quindi incollarlo nello script python di cui sopra, in produzione ovviamente sarà necessario impostare un metodo per ottenere il token sui server.
Inoltre, in produzione dovrai consultare il servizio di feedback di Apple e rimuovere i token dispositivo dagli utenti che hanno rimosso la tua app.
Controlla qui http://arashnorouzi.wordpress.com/2011/03/31/sending-apple-push-notifications-in-asp-net-part-1/ è esattamente quello che stai cercando. – Moon
@ froh42 Hai qualche codice soddisfacente? Voglio anche codice di esempio per l'implementazione passo dopo passo. Se avete quindi si prega di fornire a me. Grazie in anticipo. –