【问题标题】:Apple Push Notifications Service RSS FeedApple 推送通知服务 RSS 源
【发布时间】:2015-05-13 00:27:25
【问题描述】:

我正在为 ios 5.1 开发应用程序。我现在已经设置了可以从 Mac 程序“pushmebaby”发送的推送通知。 我的问题是如何在每次 RSS Feed 获得新条目(可能通过 PHP?)时向所有设备发送推送通知?

谢谢!

【问题讨论】:

    标签: iphone xcode apple-push-notifications


    【解决方案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
    

    【讨论】:

    • 是的,没错,但我还需要 php 脚本之类的东西来发送这些推送消息...?
    • 正确。我在 Visual Studio 2010 上有 2 个解决方案,一个在 vb 中,一个是 C#。你想看看他们吗?还是你想要它在 php 中?
    • 是的,我愿意。你只需要告诉我我需要改变什么
    • 谢谢!但是我该如何保存这个以及我必须将它上传到哪里?
    • 基本上这就是推送通知的工作原理。您需要证书、设备 ID 和消息(作为 json 字符串)。如果您发送推送通知作为测试,则将其发送到沙箱服务器(开发人员服务器),否则将其发送到普通服务器。您将使用服务器打开一个流并首先发送证书。服务器将向您发送他们的证书,您并不真正关心。
    猜你喜欢
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多