PushSharp: Android GCM推送通知接收但无推送消息

3
我正在使用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个回答

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在解析意图的额外信息时寻找“message”、“msgcnt”或“soundname”。所以,在我的情况下,这就是解决方案:
Message message = new Message.Builder().addData("message", notif.getAlert()).build();

希望这能帮助到某个人


1

0

alert 改为 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)
            );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接