C# PushSharp:收到Android GCM推送通知,但没有推送消息
我正在使用PushSharp库从我的应用程序发送推送通知C# PushSharp:收到Android GCM推送通知,但没有推送消息,c#,push-notification,google-cloud-messaging,C#,Push Notification,Google Cloud Messaging,我正在使用PushSharp库从我的应用程序发送推送通知 PushService push = new PushService(); var reg_id_d = "APA91bETd-LsqnZjA-HKrnBOY3FbEhmWchpiwuhRkiv4gUdGDuvwDRB7YURICZ131XppDAUNUBLGe_vEPkQ-JR8UaVX7Y-NCkEfastCBLIYcUoFtt5cPafeKXHywi0WGDYW33ZQqr3oy"; var project_id_d = "4828
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发送通知。它正在按预期工作。因此,我不确定我上面的代码中有什么错误。有人能帮我一下吗?我试过使用不同的代码。。但是这些都不起作用
最后我试了一下,效果很好 我遇到了同样的问题,我收到了一条空消息。我的代码有点不同,我使用的是不同的库:客户端用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在解析意图中的额外内容时查找“message”、“msgcnt”或“soundname”。这就是我的解决方案:
Message message = new Message.Builder().addData("message", notif.getAlert()).build();
希望这将有助于某人将警报更改为消息,请参阅下面的代码以供参考:
////---------------------------
//// 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)
);