我正在寻找从 VB.NET 应用程序使用 PushSharp 的答案,并且能够从各种来源拼凑一些代码。那里没有连接回调并使用库发送通知的完整示例。为了节省时间,我认为发布我的代码可以帮助其他人
最佳答案
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 - VB.NET 的示例 PushSharp 代码(苹果),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096936/