2012-04-02 149 views
1

我正在爲ios 5.1開發應用程序。我現在已經設置了可以從Mac程序「pushmebaby」發送的推送通知。 我的問題是,如何每次RSS Feed獲取新條目(可能通過PHP?)向所有設備發送推送通知?Apple推送通知服務RSS提要

謝謝!

回答

1

從我的理解,你需要跟蹤所有設備ID(如何?我個人創建了一個數據庫)。當用戶首次安裝應用程序並註冊通知服務時,應用程序應將設備ID和用戶名(某種類型的密鑰)發送給您的服務器。

VB發送推送通知ANPSLibrary這只是我爲發送推送通知而創建的函數,您需要調用此函數以便發送通知。

Imports System.Net 
Imports System.IO 
Imports System.Text 
Imports System.Security.Cryptography.X509Certificates 
Imports System.Security.Cryptography 
Imports System.Net.Security 
Imports System.Net.Sockets 
Imports System.Threading 
Imports System.Linq 
Imports System.Collections.Generic 
Imports System.Runtime.Serialization 
Imports System 





Public Class PushNotification 



    'Send PushNotifications 
    '///sanbox is true, if we are developing, and false if we are out of developing 
    '///testDeviceToken is the id of the device you want to send the push notfication to 
    '///Message is the message we want the to send to the device 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    Public Shared Function sendrequest(ByVal sandbox As Boolean, ByVal testDeviceToken As String, ByVal Message As String) 

     Dim strHost As String 
     Dim strP12FileName As String 
     Dim strP12FilePassword As String 
     Dim strJsonMsg As String 
     Dim certificate As X509Certificate2 
     Dim certificateCollection As X509CertificateCollection 
     Dim nPort As Integer = 2195 
     Dim apnsClient As TcpClient 
     Dim apnsStream As SslStream 


     Try 
      ' Sets the Host to the correct server. 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
      If sandbox = True Then 
       strHost = "gateway.sandbox.push.apple.com" 
      Else 
       strHost = "gateway.push.apple.com" 
      End If 


      'The path of the certificate 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
      strP12FileName = "C:\Users\filelocation" 
      strP12FilePassword = "password" 

      'Putting Message in json format 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
      strJsonMsg = "{""aps"":{""alert"":""" & Message & """,""badge"":1}}" 


      certificate = New X509Certificate2(strP12FileName, strP12FilePassword) 
      certificateCollection = New X509CertificateCollection 
      certificateCollection.Add(certificate) 

      'builds connection 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

      apnsClient = New TcpClient(strHost, nPort) 
      apnsStream = New SslStream(apnsClient.GetStream(), True, _ 
             New RemoteCertificateValidationCallback(AddressOf validateServerCertificate), _ 
             New LocalCertificateSelectionCallback(AddressOf selectLocalCertificate)) 

      apnsStream.AuthenticateAsClient(strHost, certificateCollection, System.Security.Authentication.SslProtocols.Ssl3, False) 

      'Turns everything in Bytes 
      '------------------------------------------------------------------------------------------------------------------------- 

      'Cannot be more than Binary size of 32 
      Dim DeviceToken((testDeviceToken.Length/2) - 1) As Byte 
      For i As Integer = 0 To 31 
       DeviceToken(i) = Byte.Parse(testDeviceToken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber) 
      Next 


      'Cannot be more than Binary size of 256 
      Dim payload() As Byte = Encoding.UTF8.GetBytes(strJsonMsg) 

      Dim DeviceTokenSize() As Byte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt16(DeviceToken.Length))) 
      Dim payloadSize() As Byte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt16(payload.Length))) 

      'Creates a Byte Array 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
      Dim NotificationSize As Integer = 1 + DeviceTokenSize.Length + DeviceToken.Length + payloadSize.Length + payload.Length 
      Dim Notification(NotificationSize) As Byte 

      Notification(0) = 0 
      Buffer.BlockCopy(DeviceTokenSize, 0, Notification, 1, DeviceTokenSize.Length) 
      Buffer.BlockCopy(DeviceToken, 0, Notification, 1 + DeviceTokenSize.Length, DeviceToken.Length) 
      Buffer.BlockCopy(payloadSize, 0, Notification, 1 + DeviceTokenSize.Length + DeviceToken.Length, payloadSize.Length) 
      Buffer.BlockCopy(payload, 0, Notification, 1 + DeviceTokenSize.Length + DeviceToken.Length + payloadSize.Length, payload.Length) 

      'Sends the Notification and closes and stream 
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
      apnsStream.Write(Notification) 
      apnsStream.Close() 
     Catch 




     Finally 
      'cleaning 
      strHost = Nothing 
      strP12FileName = Nothing 
      strP12FilePassword = Nothing 
      strJsonMsg = Nothing 
      certificate = Nothing 
      certificateCollection = Nothing 
      nPort = Nothing 
      apnsClient = Nothing 
      apnsStream = Nothing 
     End Try 
     Return True 

    End Function 
    'This is needed for RemoteCertificateValidationCallback 
    Public Shared Function validateServerCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) 

     Return True 'Dont care about server's cert 

    End Function 
    'This is needed for LocalCertificateSelectionCallback 
    Public Shared Function selectLocalCertificate(ByVal sender As Object, ByVal targetHost As String, ByVal localCertificates As X509CertificateCollection, _ 
     ByVal remoteCertificate As X509Certificate, ByVal acceptableIssuers As String()) 

     Dim certificate As X509Certificate2 
     certificate = New X509Certificate2("C:\Users\filelocation", "password") 
     Return certificate 

    End Function 

End Class 

VB驅動器//本質上創建一個窗體,帶有2個文本框,一個用於輸入設備令牌,另一個用於消息。然後在下面創建驅動程序

Imports ANPSLibrary 
Public Class Form1 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 

    End Sub 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 


     PushNotification.sendrequest(True, txtToken.Text, txtMsg.Text) 
    End Sub 
End Class 
+0

是的,這是正確的,但我也需要類似的PHP腳本或東西來發送這些推消息...? – user1110365 2012-04-02 20:48:16

+0

正確。我在Visual Studio 2010上有兩個解決方案,一個是vb,一個是C#。你想看看他們嗎?或者你想在PHP? – 2012-04-02 20:52:17

+0

是的,我喜歡。你只需要告訴我我需要更換什麼 – user1110365 2012-04-02 20:55:52