2015-02-11 179 views
1

我最近实施了PushSharp到一个应用程序,但对于我的生活我无法弄清楚为什么通知没有出现在设备上。PushSharp GCM消息传递

以下是放在一起测试行为的方法:

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密钥,虽然使用。

任何帮助将不胜感激。

+0

您是否已将包名更改为您的包名?例如,BroadcastReceiver。 – ztan 2015-02-11 17:30:02

+0

您是指服务器还是应用程序?如上所述,我看不到任何文件,这将表明我需要更新包名 – 2015-02-11 22:27:35

+0

从这个页面:https://github.com/Redth/PushSharp'看看PushService.cs文件中示例项目。您可以将大部分此类复制到您自己的应用程序中,但同样要确保在适用的情况下(您需要更改BroadcastReceiver属性)替换您自己的软件包名称。 ' – ztan 2015-02-11 22:34:00

回答

0

从我的经验,我面对这个问题,3倍,在过去的两年里:)

第一次:我不得不重新生成新的GCM API密钥,用它和它的工作。

第二次:第一个解决方案没有工作,我与android开发人员仔细检查了他的代码,结果发现他的代码有问题。

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

+0

我尝试了你建议的3种解决方案,但没有任何效果。你有其他解决方案吗?我在 – 2015-08-19 12:11:04

+0

这个问题上面对完全相同的问题我不确定你的情况是否与这一个完全一样,也许如果你用你使用的代码在新线程上发布你的问题将会是最好的 – 2015-08-19 12:24:44

+0

我的问题有已经解决了,就像你提到的第二种情况。问题出在Android应用程序中 – 2015-08-20 05:46:45

0

尝试使用消息而不是提醒。

.WithJson(@"{""message"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}")); 

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

问候,

0

对于PushSharp 4.0,你可以去这样

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用户。