2012-11-03 32 views
1

我想弄清楚如何獲得Google GCM響應(即NotRegistered錯誤),以便我可以從用戶帳戶中刪除GCM信息。如何使用PushSharp獲取GCM錯誤消息'NotRegistered'

GCM Archetecture Overview(關於從GCM註銷設備),他們聲明「只有當GCM服務器嘗試向設備發送消息並且設備回答應用程序被卸載或它沒有時纔會註冊一個配置爲接收com.google.android.c2dm.intent.RECEIVE意圖的廣播接收器,此時,服務器應將該設備標記爲未註冊(服務器將收到NotRegistered錯誤)。

PushSharp for Mono for Android;如何在嘗試向用戶發送消息時收到Google Response,而您收到Google的「未註冊錯誤」?要發送消息,我有這樣的代碼:

var push = new PushService(); 

    // setup channel settings: sender id, access key, registration package name 
    var settings = new GcmPushChannelSettings(<>, <>, <PACKAGE>); 
    push.StartGoogleCloudMessagingPushService(settings); 
    var android = NotificationFactory.AndroidGcm(); 
    android = android.ForDeviceRegistrationId(GCM_Id); 
     push.QueueNotification(android.WithJson("{\"alert\":\"" + message + "\",\"URL\":\"" + URL + "\"}")); 

我如何從谷歌迴應知道1.消息通過2.應用程序卸載或3去獲得NotRegistered錯誤,讓我可能會從用戶帳戶中刪除GCM ID?

期待着任何幫助,我可以爲此獲得。上面的代碼使用PushSharp for Mono for Android(MonoDroid),並且它可以完美地向用戶發送消息。 PushSharp是驚人的,我強烈建議它通過GCM發送消息給你的用戶。

回答

1

您需要訂閱PushService上屬於Events屬性的事件。

然後,您將能夠在事件處理程序中收到響應。

 //Create our service  
     PushService push = new PushService(); 

     //Wire up the events 
     push.Events.OnDeviceSubscriptionExpired += new Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired); 
     push.Events.OnDeviceSubscriptionIdChanged += new Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged); 
     push.Events.OnChannelException += new Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException); 
     push.Events.OnNotificationSendFailure += new Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure); 
     push.Events.OnNotificationSent += new Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent); 
     push.Events.OnChannelCreated += new Common.ChannelEvents.ChannelCreatedDelegate(Events_OnChannelCreated); 
     push.Events.OnChannelDestroyed += new Common.ChannelEvents.ChannelDestroyedDelegate(Events_OnChannelDestroyed); 

在您的事件處理程序中,您可以檢查異常並確定是否應該從用戶帳戶中刪除設備ID。

 static void Events_OnNotificationSendFailure(Common.Notification notification, Exception notificationFailureException) 
     { 
      // Remove device id 
      Console.WriteLine("Failure: " + notification.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + notification.ToString()); 
     } 
相關問題