【问题标题】:PushSharp:Android GCM Push Notification received without push messagePushSharp:收到没有推送消息的Android GCM推送通知
【发布时间】:2012-10-27 10:25:54
【问题描述】:

我正在使用 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 发送通知。它按预期工作。所以,我不确定我上面的代码有什么问题。谁能帮我解决这个问题?

【问题讨论】:

    标签: c# push-notification google-cloud-messaging


    【解决方案1】:

    我尝试使用周围可用的不同代码..但这些都不起作用..

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

    【讨论】:

      【解决方案2】:

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

      希望这会对某人有所帮助

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-27
          • 2016-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多