PushSharp:在没有推送消息的情况下收到Android GCM推送通知

时间:2012-11-07 11:07:19

标签: 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 = "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发送通知。它按预期工作。所以,我不确定上面的代码有什么问题。有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:1)

我尝试使用可用的不同代码..但这些都没有工作..

最后我尝试了https://stackoverflow.com/a/11651066/1005741,它就像一个魅力!

答案 1 :(得分:1)

我遇到了同样的问题,我收到了一条空消息。我的代码有点不同,我使用的是不同的库:客户端用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();

希望这会有所帮助

答案 2 :(得分:0)

提醒更改为消息,请参阅以下代码供您参考:

        ////---------------------------
        //// 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)
            );