2012-04-02 5 views
1

ios 5.1 용 응용 프로그램을 개발 중입니다. 이제 Mac 프로그램 "pushmebaby"에서 보낼 수있는 푸시 알림을 설정했습니다. 제 질문은 RSS 피드가 새로운 항목을 얻을 때마다 (예를 들어 PHP를 통해?) 모든 장치에 푸시 알림을 보내는 방법은 무엇입니까?Apple 푸시 알림 서비스 RSS 피드

감사합니다.

답변

1

제 생각에 모든 장치 ID를 추적해야합니다 (개인적으로 데이터베이스를 작성한 방법). 사용자가 먼저 앱을 설치하고 알림 서비스에 등록하면 앱에서 기기 ID와 사용자 이름 (일부 키 유형)을 서버로 보내야합니다.

VB 푸시 알림 보내기 ANPSLibrary 이것은 푸시 알림을 보내기 위해 만든 함수 일 뿐이므로 알림을 보내려면이 함수를 inorder로 호출해야합니다.

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

+0

이 맞습니다. Visual Studio 2010에는 2 가지 솔루션이 있는데 하나는 VB에서, 하나는 C#입니다. 그들을 바라 볼래요? 또는 PHP에서 원합니까? –

+0

네, 있습니다. 당신은 단지 내가 바꿀 필요가있는 것을 말해야 만합니다. – user1110365