我遵循 PushSharp 项目的 wiki 并编写此代码以向我的设备发送通知。
Imports PushSharp
Imports PushSharp.Android
Imports PushSharp.Core
Module Module1
Sub Main()
Console.WriteLine("Start notification : ")
Dim push As New PushBroker()
AddHandler push.OnDeviceSubscriptionExpired, AddressOf DeviceSubscriptionChanged
AddHandler push.OnChannelException, AddressOf ChannelException
AddHandler push.OnServiceException, AddressOf ServiceException
AddHandler push.OnNotificationFailed, AddressOf NotificationFailed
AddHandler push.OnDeviceSubscriptionExpired, AddressOf DeviceSubscriptionExpired
AddHandler push.OnDeviceSubscriptionChanged, AddressOf DeviceSubscriptionChanged
AddHandler push.OnChannelCreated, AddressOf ChannelCreated
AddHandler push.OnChannelDestroyed, AddressOf ChannelDestroyed
push.RegisterGcmService(New GcmPushChannelSettings("AIzaSyCcyNj2Q8bHJqJ-gFR6N3CFtM_VJpv9lIE"))
push.QueueNotification(New GcmNotification().ForDeviceRegistrationId("APA91bGPD7eT7_MEqnL5D23BxihL3-4JkpFGpKy_2A2MNIJHFyEqhSM0iFVMRortNg_394VjsXqyuP0vbPbTQcWL-3ab_4mp-rUn4ypfkntqXfgurBeXOc6M5j25ewuclXuhezhV5yULQzpiJWoOlhYFn28Yx8iJRTA5jup4lKYBo7uVObSsLzs").WithJson("{""alert"":""Hello World2!"",""badge"":6,""sound"":""sound.caf""}"))
Console.WriteLine("Waiting for Queue to Finish...")
'Stop and wait for the queues to drains
push.StopAllServices(True)
Console.WriteLine("Queue Finished, press return to exit...")
Console.ReadLine()
End Sub
Sub DeviceSubscriptionChanged(sender As Object, oldSubscriptionId As String, newSubscriptionId As String, notification As INotification)
'Currently this event will only ever happen for Android GCM
Console.WriteLine("Device Registration Changed: Old-> " & oldSubscriptionId & " New-> " & newSubscriptionId & " -> " & Convert.ToString(notification))
End Sub
Sub NotificationSent(sender As Object, notification As INotification)
Console.WriteLine("Sent: " & Convert.ToString(sender) & " -> " & Convert.ToString(notification))
End Sub
Sub NotificationFailed(sender As Object, notification As INotification, notificationFailureException As Exception)
Console.WriteLine("Failure: " & Convert.ToString(sender) & " -> " & Convert.ToString(notificationFailureException.Message) & " -> " & Convert.ToString(notification))
End Sub
Sub ChannelException(sender As Object, channel As IPushChannel, exception As Exception)
Console.WriteLine("Channel Exception: " & Convert.ToString(sender) & " -> " & Convert.ToString(exception))
End Sub
Sub ServiceException(sender As Object, exception As Exception)
Console.WriteLine("Channel Exception: " & Convert.ToString(sender) & " -> " & Convert.ToString(exception))
End Sub
Sub DeviceSubscriptionExpired(sender As Object, expiredDeviceSubscriptionId As String, timestamp As DateTime, notification As INotification)
Console.WriteLine("Device Subscription Expired: " & Convert.ToString(sender) & " -> " & expiredDeviceSubscriptionId)
End Sub
Sub ChannelDestroyed(sender As Object)
Console.WriteLine("Channel Destroyed for: " & Convert.ToString(sender))
End Sub
Sub ChannelCreated(sender As Object, pushChannel As IPushChannel)
Console.WriteLine("Channel Created for: " & Convert.ToString(sender))
End Sub
End Module
我有一个很好的 api 密钥,我通过 phonegap 插件获得了我的注册 ID。我已经用一个小的 npm 模块测试了我的 api 密钥和我的注册 ID 以发送通知。当我午餐这段代码时,什么都没有出现(没有事件匹配)并且我的手机上没有任何通知。
请帮助我,我真的需要这个代码。