简体   繁体   English

使用gcm的android推送通知

[英]android push notification using gcm

I am developing wcf push notification service using google's GCM API. 我正在使用Google的GCM API开发wcf推送通知服务。 i created a service which sends to all the device which use my application but, i wanted to be specific to some device. 我创建了一个服务,该服务将发送到使用我的应用程序的所有设备,但是,我想特定于某个设备。 i am thinking i have to use the token i get when i register for the GCM service. 我想我必须使用注册GCM服务时获得的令牌。 but i dont know where and how to implment it. 但是我不知道在哪里以及如何实现它。 most of the online posts are in PHP and i am kind of confused when i see the codes. 大多数在线帖子都使用PHP,当我看到代码时,我有点困惑。 Any one with C# implmentation advice or in general may be? 任何具有C#实施建议的人或一般人都可以? here is my code for all the devices: 这是我所有设备的代码:

 public bool notify(string sender, string message)
    {
        var jGcmData = new JObject();
        var jData = new JObject();
        bool Value;

        jData.Add("message", message);
        jData.Add("name", sender);
        jGcmData.Add("to", "/topics/global");
        jGcmData.Add("data", jData);

        var url = new Uri("https://gcm-http.googleapis.com/gcm/send");
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.TryAddWithoutValidation(
                    "Authorization", "key=" + API_KEY);

                Task.WaitAll(client.PostAsync(url,
                    new StringContent(jGcmData.ToString(), Encoding.Default, "application/json"))
                .ContinueWith(response =>
                {
                    Console.WriteLine(response);
                    Console.WriteLine("Message sent: check the client device notification tray.");
                }));
            }
            Value = true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Unable to send GCM message:");
            Console.Error.WriteLine(e.StackTrace);
            Value = false;
        }
        return Value;
    }

thanks in advance! 提前致谢!

1st of all I think you should not reinvent the wheel and include a Push messaging library to do all the redundant work. 首先,我认为您不应该重新发明轮子,而应该包括Push消息库来完成所有多余的工作。

I use PushSharp 我使用PushSharp

Then everything is cake. 那一切都是蛋糕。

Declare the following handler class Using the SendGCMNotification method just throw an object to serialize and a specific user's push messaging id . 声明以下处理程序类使用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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Android和C#中使用GCM推送通知 - Unable to Push notification by using GCM in android and C# 用于GCM的服务器将通知推送到C#中的android - Server for GCM push notification to android in C# PushSharp:在没有推送消息的情况下收到Android GCM推送通知 - PushSharp:Android GCM Push Notification received without push message 使用 c# .net 发送带有图像的 GCM 推送通知(适用于 android 应用程序) - sending GCM push notification with image using c# .net (for android application) 使用C#asp.net的GCM推送通知在Android设备中将null用作通知 - GCM push notification with C# asp.net giving null as notification in android devices 使用GCM和网络服务+数据库数据在多部Android手机上进行通知 - Notification on multiple android phones using GCM and web services + database data 推送通知(使用Andriod的GCM)在本地主机中运行正常,但在生产服务器中无法运行 - push notification (GCM using Andriod) working fine in Local host but failed to work in production server 在 Android 设备上使用 FCM 发送推送通知 - sending push notification using FCM on android device C#Android GCM通知禁用声音 - c# android GCM notification disable sound GCM推送通知服务器端为asp.net - GCM push notification Server side for asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM