用于 VB.NET (apple) 的示例 PushSharp 代码

Posted

技术标签:

【中文标题】用于 VB.NET (apple) 的示例 PushSharp 代码【英文标题】:Sample PushSharp code for VB.NET (apple) 【发布时间】:2013-02-26 18:48:51 【问题描述】:

我正在寻找从 VB.NET 应用程序中使用 PushSharp 的答案,并且能够将来自各种来源的一些代码拼凑在一起。没有一个完整的例子来连接回调并使用库发送通知。为了节省时间,我认为发布我的代码会帮助别人

【问题讨论】:

【参考方案1】:
    Imports PushSharp.PushService
    Imports PushSharp.Apple
    Imports PushSharp.android
    Imports PushSharp.WindowsPhone
    Imports PushSharp.Windows

    Protected _push As New PushSharp.PushService

    Private Sub doSend()
      initializePush()
      SendMobileNotifications()
      deinitializePush()
    End Sub

          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
          ''
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          Private Shared Sub Events_OnDeviceSubscriptionIdChanged(platform As PushSharp.Common.PlatformType, oldDeviceInfo As String, newDeviceInfo As String, notification As PushSharp.Common.Notification)
            'Currently this event will only ever happen for Android GCM
            Debug.Print("Device Registration Changed:  Old-> " & oldDeviceInfo & "  New-> " & newDeviceInfo)
          End Sub

          Private Shared Sub Events_OnNotificationSent(notification As PushSharp.Common.Notification)
            debug.print("Sent: " & notification.Platform.ToString() & " -> " & notification.ToString())
          End Sub

          Private Shared Sub Events_OnNotificationSendFailure(notification As PushSharp.Common.Notification, notificationFailureException As Exception)
            debug.print("Failure: " & notification.Platform.ToString() & " -> " & Convert.ToString(notificationFailureException.Message) & " -> " & notification.ToString())
          End Sub

          Private Shared Sub Events_OnChannelException(exception As Exception, platformType As PushSharp.Common.PlatformType, notification As PushSharp.Common.Notification)
            debug.print("Channel Exception: " & platformType.ToString() & " -> " & exception.ToString())
          End Sub

          Private Shared Sub Events_OnDeviceSubscriptionExpired(platform As PushSharp.Common.PlatformType, deviceInfo As String, notification As PushSharp.Common.Notification)
            debug.print("Device Subscription Expired: " & platform.ToString() & " -> " & deviceInfo)
          End Sub

          Private Shared Sub Events_OnChannelDestroyed(platformType As PushSharp.Common.PlatformType, newChannelCount As Integer)
            debug.print("Channel Destroyed for: " & platformType.ToString() & " Channel Count: " & newChannelCount)
          End Sub

          Private Shared Sub Events_OnChannelCreated(platformType As PushSharp.Common.PlatformType, newChannelCount As Integer)
            debug.print("Channel Created for: " & platformType.ToString() & " Channel Count: " & newChannelCount)
          End Sub
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
          ''
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          Private Sub initializePush()
            ' Wire up the events
            _push = New PushSharp.PushService
            AddHandler _push.Events.OnDeviceSubscriptionExpired, AddressOf Events_OnDeviceSubscriptionExpired
            AddHandler _push.Events.OnDeviceSubscriptionIdChanged, AddressOf Events_OnDeviceSubscriptionIdChanged
            AddHandler _push.Events.OnChannelException, AddressOf Events_OnChannelException
            AddHandler _push.Events.OnNotificationSendFailure, AddressOf Events_OnNotificationSendFailure
            AddHandler _push.Events.OnNotificationSent, AddressOf Events_OnNotificationSent
            AddHandler _push.Events.OnChannelCreated, AddressOf Events_OnChannelCreated
            AddHandler _push.Events.OnChannelDestroyed, AddressOf Events_OnChannelDestroyed

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ''Configure and start Apple APNS
            '' IMPORTANT: Make sure you use the right Push certificate.  Apple allows you to generate one for connecting to Sandbox,
            ''   and one for connecting to Production.  You must use the right one, to match the provisioning profile you build your
            ''   app with!
            Dim appleCert() As Byte = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../PushSharp/Resources/DocketTrak_Development.p12"))
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ''IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server 
            ''  (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
            ''  If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
            ''  (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
            _push.StartApplePushService(New ApplePushChannelSettings(False, appleCert, "trakDock3%", False))
          End Sub
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
          ''
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          Private Sub deinitializePush()
            _push.StopAllServices(True)
            _push = Nothing
          End Sub
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
          ''
          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
          Private Sub SendMobileNotifications()
            Dim rst As SqlClient.SqlDataReader
            rst = local_sql.cnExecute(strSql)
            Do While rst.Read()
              Dim apple As AppleNotification = PushSharp.NotificationFactory.Apple()
              PushSharp.FluentNotification.ForDeviceToken(apple, rst("device_id"))
              PushSharp.FluentNotification.WithAlert(apple, "alert Test!")
              PushSharp.FluentNotification.WithSound(apple, "default")
              PushSharp.FluentNotification.WithBadge(apple, 5)
              _push.QueueNotification(apple)
            Loop
          End Sub

【讨论】:

以上是关于用于 VB.NET (apple) 的示例 PushSharp 代码的主要内容,如果未能解决你的问题,请参考以下文章

VB.net Socket Udp收发数据包示例源码

哪个 IDE 用于基于 .NET 框架 2.5 构建的 VB.NET 代码?

用于 VB(5 或 .NET)的基本和简单的图像处理库

VB.Net Convert ToDecimal 不适用于逗号

用于 SQL Server 和 VB.NET 应用程序的 MultipleActiveResultSets

用于 android 的 vb.net 中的 pushSharp