我最近在一个应用程序中实现了PushSharp,但我一直不明白为什么通知不会出现在设备上。
下面是一个用来测试行为的方法:
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密钥才能发送通知。
但是,除了api密钥之外,所有的密钥都被使用了。
任何帮助都将不胜感激。
发布于 2015-03-09 23:37:07
根据我的经验,我在过去两年中遇到过这个问题三次:)
第一次:我必须重新生成一个新的GCM API密钥,使用它,它起作用了。
第二次:第一个解决方案不起作用,我和android开发人员检查了他的代码,结果发现他的代码有问题。
第三次:问题出在设备上,我不得不重启设备,之后立即收到通知。
发布于 2015-06-04 15:47:29
尝试使用message而不是alert。
.WithJson(@"{""message"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}"));与我的情况一样,服务(就像您的服务一样)正在发送警报,但客户端设备正在等待一条消息。
致以敬意,
发布于 2016-12-01 22:46:42
对于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 + "\"" +
"}")
});我没有测试自定义数据值是否到达,但通知是:)以" your“开头的项是您的动态参数,如传递给该方法的参数等。这个问题很久以前就提出了,但我现在才开始使用pushsharp:)希望这对PushSharp用户有帮助。
https://stackoverflow.com/questions/28446152
复制相似问题