我正在使用PushSharp库从我的应用程序发送推送通知。
PushService push = new PushService();
var reg_id_d = "APA91bETd-LsqnZjA-HKrnBOY3FbEhmWchpiwuhRkiv4gUdGDuvwDRB7YURICZ131XppDAUNUBLGe_vEPkQ-JR8UaVX7Y-NCkEfastCBLIYcUoFtt5cPafeKXHywi0WGDYW33ZQqr3oy";
var project_id_d = "482885626272";
var api_key_d = "AIzaSyAbh7R5KQR3KM7W_y-yS-Ao-JNiihNz7tE"; // "AIzaSyDcKfuW77GTwA46L6sqD41YhGf2j5S8o2w";
var package_name_d = "com.get.deviceid";
push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings(project_id_d, api_key_d, package_name_d));
push.QueueNotification(NotificationFactory.AndroidGcm()
.ForDeviceRegistrationId(reg_id_d)
.WithCollapseKey("NONE")
.WithJson("{\"alert\":\"Alert Text!\",\"badge\":\"1\"}"));我在我的设备上收到通知,但消息为空。
我试着用C#中可用的服务器代码发送GCM推送通知,但得到了同样的空消息问题。
我尝试使用PHP发送通知。而且它的运行情况与预期一致。因此,我不确定上面代码中的错误所在。有人能在这方面帮我一下吗?
发布于 2012-11-11 03:32:40
我试着使用不同的代码..但是这些都不起作用..
最后,我尝试了一下https://stackoverflow.com/a/11651066/1005741,它的效果非常棒!
发布于 2013-07-01 23:02:02
我遇到了同样的问题,我收到了一条空消息。我的代码有点不同,我使用的是不同的库:客户端用phonegap pushPlugin包装,服务器代码如下:
...
// com.google.android.gcm.server.Sender.Sender(String key)
gcmSender = new Sender(androidAPIkey);
// com.google.android.gcm.server.Message
Message message = new Message.Builder().addData("alert", "test message" /*notif.getAlert()*/).build();
Result result = gcmSender.sendNoRetry(message, /* device token */ notif.getToken());
nr.add(result, notif.getToken());
...我的消息为空的原因是因为phonegap在解析intent中的附加内容时会查找"message“、"msgcnt”或"soundname“。所以,这就是我的解决方案:
Message message = new Message.Builder().addData("message", notif.getAlert()).build();希望这能帮助到一些人
发布于 2014-11-19 13:15:34
将警报更改为message,请参阅以下代码以供参考:
////---------------------------
//// ANDROID GCM NOTIFICATIONS
////---------------------------
////Configure and start Android GCM
////IMPORTANT: The API KEY comes from your Google APIs Console App, under the API Access section,
//// by choosing 'Create new Server key...'
//// You must ensure the 'Google Cloud Messaging for Android' service is enabled in your APIs Console
push.RegisterGcmService(new GcmPushChannelSettings("senderid", "apikey", "com.xx.m"));
//Fluent construction of an Android GCM Notification
//IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself!
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("regid")
.WithCollapseKey("score_update")
.WithJson("{\"message\":\"syy!\",\"badge\":7,\"sound\":\"sound.caf\"}")
.WithTimeToLive(108)
);https://stackoverflow.com/questions/13268460
复制相似问题