2

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

4

1 回答 1

4
    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
于 2013-02-26T18:48:51.697 回答