0

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

droidBomb
  • 850
  • 5
  • 8
manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
  • code for generating notification through notification channel is missing. – Sahil Oct 05 '18 at 05:10
  • @Sahil Thanks, what do you mean, please? can you include in an answer? thanks – manuelBetancurt Oct 05 '18 at 05:11
  • @MaKo you need to create notification from code check her for example code working in oreo https://stackoverflow.com/a/51129304/7666442 – AskNilesh Oct 05 '18 at 05:16
  • @MaKo refer https://stackoverflow.com/a/43093261/5906447 also you notification channels were introduced from Oreo so make sure you take care of previous API versions also explained on the post. :) – Sahil Oct 05 '18 at 05:17

3 Answers3

1

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.

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
0

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());
umesh shakya
  • 237
  • 2
  • 12
0

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\" } }"
}
manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216