在PushSharp 4.0中构造GCM消息 [英] Structuring GCM messages in PushSharp 4.0

查看:201
本文介绍了在PushSharp 4.0中构造GCM消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于应该如何使用PushSharp为GCM推送通知构造消息正文,我有些困惑. GitHub存储库中的文档以及测试文件显示了如下所示的消息结构:

I'm somewhat confused in how I'm supposed to structure the message body for a GCM push notification using PushSharp. The docs as well as the test files in the GitHub repo show what looks to be the message structure as this:

            broker.QueueNotification (new GcmNotification {
                RegistrationIds = new List<string> { 
                    regId
                },
                Data = JObject.Parse ("{ \"somekey\" : \"somevalue\" }")
            });

我一直在使用Postman进行测试,在该测试中,我将以下JSON格式的消息发送到 https://gcm-http.googleapis.com/gcm/send .

I've been using Postman for testing in which I send a message in the following JSON format to https://gcm-http.googleapis.com/gcm/send.

{
    "to": "000-0000-mytoken-foo",
    "notification" : {
        "title" : "Push Test Notification",
        "body" : "GCM Test Push",
        "icon" : "icon",
        "color" : "#FF4081"
    }

}

框架是否已经处理了消息的'to''notification'部分?根据示例,我似乎似乎只需要输入通知对象的key:value对,但似乎无法找到声明的位置或文档中实际消息的示例.我使用的是PushSharp的最新版本(4.x).

Is the 'to' and the 'notification' sections of the message already taken care of by the framework? Based off the examples I've seen it seems as though I only need to enter the key:value pairs of the notification object but I can't seem to find where that is stated or an example of an actual message in the docs. I'm using the newest version (4.x) of PushSharp.

推荐答案

您好,您可以像这样

var config = new GcmConfiguration("senderKey", "apiKey", null);
config.GcmUrl = "https://fcm.googleapis.com/fcm/send"; //!!!!!gmc changed to fcm;)

var gcmBroker = new GcmServiceBroker(config);
gcmBroker.Start();

_gcmBroker.QueueNotification(new GcmNotification
                {
                    RegistrationIds = new List<string> { "YourDeviceToken" },
                    Notification = JObject.Parse(
                        "{" +
                            "\"title\" : \"" + yourMessageTitle + "\"," +
                            "\"body\" : \"" + yourMessageBody + "\"," +
                            "\"sound\" : \"mySound.caf\"" +
                        "}") ,
                    Data = JObject.Parse(
                        "{" +                            
                            "\"CustomDataKey1\" : \"" + yourCustomDataValue1 + "\"," +
                            "\"CustomDataKey2\" : \"" + yourCustomDataValue2 + "\"" +
                        "}")
                });

我没有测试自定义数据值是否到来,但通知确实到达了:)以您的"开头的项是您的动态参数,例如您传递给该方法的参数等.很久以前我问过这个问题,但是我开始了立即使用pushsharp :)希望这对PushSharp用户有所帮助.

I did not test if the custom data values are arriving but the notification does:) The items starts with "your" are your dynamic arguments like parameters that you pass to that method etc. The question asked long time ago but i started to use pushsharp just now :) Hope this helps to PushSharp users.

这篇关于在PushSharp 4.0中构造GCM消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆