使用gcm的android推送通知

时间:2015-11-05 09:25:49

标签: c# android google-cloud-messaging

我正在使用google的GCM API开发wcf推送通知服务。我创建了一个服务,发送到使用我的应用程序的所有设备,但我想特定于某些设备。我想我必须使用我在注册GCM服务时获得的令牌。但我不知道在哪里以及如何实施它。大多数在线帖子都是用PHP编写的,当我看到代码时,我有点困惑。任何一个有C#建议或一般的人可能会? 这是我所有设备的代码:

NotifyService

提前感谢!

1 个答案:

答案 0 :(得分:0)

首先,我认为你不应该重新发明轮子并包括推送消息库来完成所有冗余工作。

I use PushSharp

然后一切都是蛋糕。

声明以下处理程序类 使用SendGCMNotification方法只需抛出一个对象来序列化和特定用户的推送消息ID。

    public class PushNotificationHandler : IDisposable
{
    private static readonly string googleApiKey;
    private static PushBroker pushBrokerInstance;
    static PushNotificationHandler()
    {
        googleApiKey = ConfigurationManager.AppSettings["GoogleAPIKey"].ToString();
        pushBrokerInstance = new PushBroker();
        pushBrokerInstance.RegisterGcmService(new GcmPushChannelSettings(googleApiKey));

    }
    public static void SendGCMNotification(Notification messageObj, String CloudMessagingId)
    {
        String Content = Newtonsoft.Json.JsonConvert.SerializeObject(messageObj);
        pushBrokerInstance.QueueNotification(new GcmNotification().ForDeviceRegistrationId(CloudMessagingId).WithJson(Content));
    }
}