c# - 如何使用 PushSharp 获取 GCM 错误消息 'NotRegistered'

标签 c# android xamarin.android

我正在尝试弄清楚如何获得 Google GCM 响应(即 NotRegistered 错误),以便我可以从用户帐户中删除 GCM 信息。

来自 GCM Archetecture Overview (关于从 GCM 取消注册设备),他们声明“只有当 GCM 服务器尝试向设备发送消息并且设备回答应用程序已卸载或没有配置接收广播接收器时才会取消注册com.google.android.c2dm.intent.RECEIVE Intent 。此时,您的服务器应将设备标记为未注册(服务器将收到 NotRegistered 错误)。”

在 PushSharp for Mono for Android 中;尝试向用户发送消息时如何获得 Google 响应,却收到来自 Google 的“NotRegistered 错误”?要发送消息,我有以下代码:

    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 + "\"}")); 

我如何从 Google 获得响应以了解是否 1. 消息已通过 2. 应用程序已卸载或 3. 收到 NotRegistered 错误以便我可以从用户帐户中删除 GCM Id?

期待为此获得任何帮助。上面的代码使用 PushSharp for Mono for Android (MonoDroid),它可以完美地向用户发送消息。 PushSharp太棒了,我强烈推荐它通过 GCM 向您的用户发送消息。

最佳答案

您需要订阅附加到 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());
        }

关于c# - 如何使用 PushSharp 获取 GCM 错误消息 'NotRegistered',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213695/

相关文章:

android - FragmentPagerAdapter 的麻烦和困境 : Constructor Undefined

sqlite - 每次更新应用程序时,我都需要创建一个新的SQLite数据库吗?

c# - 是否可以在 gsoap 的单独线程中执行函数?

c# - 开发非常基本的 Windows 8.1 应用程序。如何从后面的 xaml 代码执行验证

c# - 运行时属性的 Web API 条件序列化

c# - 如何在 .NET CF C# 3.5 中连接到 wifi 网络

android - 从 firebase 查询数据

java - 从 AsyncTask 将数据返回到 Activity 的问题

xamarin.android - 如何使用 MvvmCross 框架使用 MonoDroid TabActivity

c# - Android和iOS的跨平台网络状态检测——如果不是: how to implement on Android?