首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PushSharp GCM消息传递

PushSharp GCM消息传递
EN

Stack Overflow用户
提问于 2015-02-11 11:32:55
回答 3查看 3.4K关注 0票数 1

我最近在一个应用程序中实现了PushSharp,但我一直不明白为什么通知不会出现在设备上。

下面是一个用来测试行为的方法:

代码语言:javascript
复制
private void SendPushNotification()
{
    var push = new PushBroker();

    //Wire up the events for all the services that the broker registers
    push.OnNotificationSent += NotificationSent;
    push.OnChannelException += ChannelException;
    push.OnServiceException += ServiceException;
    push.OnNotificationFailed += NotificationFailed;
    push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
    push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
    push.OnChannelCreated += ChannelCreated;
    push.OnChannelDestroyed += ChannelDestroyed;

    push.RegisterGcmService(new GcmPushChannelSettings("My-API-Key-here"));

    push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("MyReallyLongRegisteredDeviceKeyHere")
              .WithJson(@"{""alert"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}"));


    //Stop and wait for the queues to drains
    push.StopAllServices();
}

此方法在应用程序启动时立即调用,因此我希望立即收到通知,但我没有这样做。

更奇怪的是,调用了事件OnNotificationSent,这将表明消息已经通过。

为了让它正常工作,我还需要配置什么吗?文档说明需要发送者id、包名和api密钥才能发送通知。

https://github.com/Redth/PushSharp/wiki/How-to-Configure-&-Send-GCM-Google-Cloud-Messaging-Push-Notifications-using-PushSharp#setting-up-pushsharp-to-send-notifications

但是,除了api密钥之外,所有的密钥都被使用了。

任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2015-03-09 23:37:07

根据我的经验,我在过去两年中遇到过这个问题三次:)

第一次:我必须重新生成一个新的GCM API密钥,使用它,它起作用了。

第二次:第一个解决方案不起作用,我和android开发人员检查了他的代码,结果发现他的代码有问题。

第三次:问题出在设备上,我不得不重启设备,之后立即收到通知。

票数 0
EN

Stack Overflow用户

发布于 2015-06-04 15:47:29

尝试使用message而不是alert。

代码语言:javascript
复制
.WithJson(@"{""message"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}"));

与我的情况一样,服务(就像您的服务一样)正在发送警报,但客户端设备正在等待一条消息。

致以敬意,

票数 0
EN

Stack Overflow用户

发布于 2016-12-01 22:46:42

对于PushSharp 4.0,您可以这样操作

代码语言:javascript
复制
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 + "\"" +
                        "}")
                });

我没有测试自定义数据值是否到达,但通知是:)以" your“开头的项是您的动态参数,如传递给该方法的参数等。这个问题很久以前就提出了,但我现在才开始使用pushsharp:)希望这对PushSharp用户有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28446152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档