【问题标题】:Structuring GCM messages in PushSharp 4.0在 PushSharp 4.0 中构建 GCM 消息
【发布时间】:2016-08-22 16:02:29
【问题描述】:

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

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

我一直在使用 Postman 进行测试,其中我以以下 JSON 格式向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' 部分是否已由框架处理?根据我看到的示例,似乎我只需要输入通知对象的键:值对,但我似乎无法找到说明的位置或文档中实际消息的示例。我正在使用 PushSharp 的最新版本 (4.x)。

【问题讨论】:

  • 你知道它是如何设置的吗?我收到了成功消息,但 Android 上没有收到通知
  • @pechar 是的,您可能想要发送通知而不是数据消息。将Data = JObject.Parse 更改为Notification = JObject.Parse。稍后我将通过示例发布更充实的答案。
  • 感谢您的回复。我实际上设法让它在晚上运行。我还是使用了Data 属性,因为Android 应用程序使用那里传递的数据处理通知。但是我想这一切都取决于您的应用程序如何处理通知。 to 也可以在GcmNotification 的单独属性中分配,方法与notification 相同
  • @pechar 你知道如何从 Push Sharp 请求令牌刷新吗?
  • 我不确定我是否理解您的意思,但通常需要在设备上进行令牌刷新。然后将新令牌发送到您的服务器。有一个异常可以捕获OnNotificationFailed,您可以在其中检查异常参数,如果它是DeviceSubscriptionExpiredException,那么您可以获得OldSubscriptionIdNewSubscriptionId。然后,您可以将数据库中的旧 ID 替换为新 ID。这里的订阅 ID 是令牌。

标签: c# android .net pushsharp


【解决方案1】:

你好,你可以这样

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 用户有所帮助。

【讨论】:

  • 错误是什么?您是否注册了 _gcmBroker 的事件?例如:_gcmBroker.OnNotificationSucceeded += notification => { //Console.WriteLine("Android Notification Sent!"); };那么你从这次活动中得到了什么? @AhmadArslan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多