0

I am trying to get the message from server as a push notification in android. I am able to hit the server and but I got null message from server. I can see notification in android with no message.

Here is my code

@Override
protected void onMessage(Context context, Intent intent) {

    if(aController == null)
        aController = (Controller) getApplicationContext();

    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");        
    aController.displayMessageOnScreen(context, message);
    // notifies user
    generateNotification(context, message);                                               
}
Ravi
  • 30,808
  • 18
  • 108
  • 168
  • 1
    Post your receiver code. – AndroidHacker Nov 23 '16 at 05:31
  • Did you pass your Device id to the server – Vadivel Nov 23 '16 at 05:33
  • Please post your complete code – AndroidHacker Nov 23 '16 at 05:33
  • `I can see notification in android with no message.` in that case there should not be anything wrong with integration, print all the keys and values you are receiving in `intent.getExtras()`, follow [this](http://stackoverflow.com/questions/14948697/print-the-contents-of-a-bundle-to-logcat) to print all the keys and values – Ravi Nov 23 '16 at 05:35
  • Google is still supporting GCM so there should not be any issue , please post your complete code. And also provide the log what your server is sending. – Tas Nov 23 '16 at 05:40
  • simply debug it at every checkpoint including source , server script , destination , narrow down the issue and post the code related to that checkpoint – Pavneet_Singh Nov 23 '16 at 05:44
  • I will recommend you to use FCM instead of GCM and still if you want to go for GCM, try this :- https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/ – Bhavnik Nov 23 '16 at 05:44
  • GCM is deprecated please use FCM. Here is a working example of FCM [https://firebase.google.com/docs/cloud-messaging/android/client](http://stackoverflow.com/questions/24769257/custom-listview-adapter-with-filter-android) – Irfan Ullah Nov 23 '16 at 05:35

1 Answers1

0

In case you are using GcmListenerService

public class MyGCMService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        if(data.containsKey("type"))
        {
            String type = data.getString("type");
            if(type.equalsIgnoreCase("qr_update"))
            {
                SharedPrefUtil.setSharedPref(getApplicationContext(), "qr", "");
            }
        }
        else
        {
            sendNotification(message);
        }
    }

    private void sendNotification(String message)
    {
        initiateNotification(message);
    }

    private void initiateNotification(String message)
    {
        Intent intent = new Intent(this, Splash.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getResources().getString(R.string.app_name))
                //.setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(message))
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 , notificationBuilder.build());
    }

}

Even though I will recommend you to use FCM as GCM is depricated

AndroidHacker
  • 3,588
  • 22
  • 44