简体   繁体   中英

PushSharp:Android GCM Push Notification received without push message

I am using PushSharp library to send push notification from my application.

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\"}"));

I am getting notification on my device but with blank message..

I have tried with sever code available in C# to send GCM push notification, but getting same problem of having blank message.

I tried using PHP to send notification. and it is working as expected. so, I am not sure what is wrong in my above code. Can anyone please help me on this?

I tried using different code available around.. but none of those were working..

finally I tried https://stackoverflow.com/a/11651066/1005741 and it works like a charm!

I encountered the same issue, where I received an empty message. My code was a bit different and i was using different libraries: the client was wrapped with phonegap pushPlugin ,and the server code is as follows :

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

The reason why my messages where empty is due to the fact that phonegap looks for "message" , "msgcnt" or "soundname" while parsing the extras from the intent. So, this was the solution in my case :

Message message = new Message.Builder().addData("message", notif.getAlert()).build();

Hope this will help someone

Change alert to message , Please see code below for your reference:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM