manuelBetancurt
manuelBetancurt

Reputation: 16158

Android push notification logs onMessageReceived but not displaying on device

I am sending some push notifications to android from AWS,

the notification process is working registering the device, and I indeed get the test notifications, but only showing on the log, not on notification bar on top of the screen like any other notification would...

 public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.

    Log.d("mako", "A From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d("mako", "B Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long-running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
//                scheduleJob();

        } else {
            // Handle message within 10 seconds
//                handleNow();

        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d("mako", "C Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

So, I send a test notification:

{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}

And I can see in my console log the message test:

10-05 14:57:08.827 23062-23296/com.sb.comm D/mako: B Message data payload: {message=test message}

But is not showing on the little pop-up, What is missing to show the actual push notification on the screen?

Cheers

Upvotes: 0

Views: 1255

Answers (3)

manuelBetancurt
manuelBetancurt

Reputation: 16158

Turns out it was to do with the formatting of the body, it is not as suggested by >>> SNS "JSON message generator"

the body should be in this format:

{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}

Upvotes: 0

Tejas Pandya
Tejas Pandya

Reputation: 4087

You have to Generate Notification on your device like this

    public void onMessageReceived(RemoteMessage remoteMessage) {

      NotificationCompat.Builder notificationBuilder = 
         new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("App Name")
        .setBadgeIconType(R.drawable.ic_notification)
        .setLargeIcon(BitmapFactory.decodeResource(
         getResources(),R.drawable.ic_notification))
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
         .setContentText(remoteMessage.getData().get("body"));



            NotificationManager notificationManager =
                    (NotificationManager)
                            getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(141, notificationBuilder.build());
        }

Here Notification Builder will get data from your server notification payload and generate a notification on device.

Upvotes: 1

umesh shakya
umesh shakya

Reputation: 237

please add these lines of code to show notification on notification bar.

 Intent intent = new Intent(this, YourActivity.class);
 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
 PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notificationBuilder = new 
    NotificationCompat.Builder(this, "my_chanel_id");
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher_app);
    notificationBuilder.setContentTitle("Titlee");
    notificationBuilder.setContentText("anyText");
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(uri);
    notificationBuilder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel("mychanelId", 
           "hyperlocal", importance);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(message_id, notificationBuilder.build());

Upvotes: 0

Related Questions