繁体   English   中英

将GCM推送通知发送到设备

[英]Send GCM Push Notification to device

我正在尝试开发Xamarin Android应用,该应用使用GCM服务发送推送通知。

我遵循了Xamarin教程页面上介绍的教程,该教程教导了如何注册GCM,请求设备令牌以及如何创建消息发送者。

一切正常,除了我想向令牌特别是x的特定设备发送推送消息,而不是向所有设备发送。

消息发送方如下所示:

    private static void messageSender(string title, string message, string summary)
    {
        var jGcmData = new JObject();
        var jData = new JObject();

        jData.Add("message", message);
        jData.Add("title", title);
        jData.Add("summary", summary);
        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.");
                        }));
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Unable to send GCM message:");
            Console.Error.WriteLine(e.StackTrace);
        }
    }

有什么帮助吗?

*client side*

    private void SendNotification()
        {
            var jGcmData = new JObject();
            var jData = new JObject();

            jData.Add("title", txtTitle.Text);
            jData.Add("message", txtMessage.Text);
            jData.Add("summary", txtSummary.Text);
            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.");
                        }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to send GCM message:");
                Console.Error.WriteLine(ex.StackTrace);
            }
        }
*phone side*

public override void OnMessageReceived(string from, Bundle data)
        {
            var message = data.GetString("message");
            var title = data.GetString("title");
            var summary = data.GetString("summary");
            Log.Debug("MyGcmListenerService", "From:    " + from);
            Log.Debug("MyGcmListenerService", "Message: " + message);
            Log.Debug("MyGcmListenerService", "Title: " + title);
            Log.Debug("MyGcmListenerService", "Summary: " + summary);
            SendNotification(message, title, summary);
        }
        void SendNotification(string message,string title,string summary)
        {

            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
            var notificationBuilder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.aalogo2)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetSubText(summary)
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent);
            var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            notificationManager.Notify(0, notificationBuilder.Build());
        }

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM