0

I am making MQTT client app using paho MQTT client dependency.

Implementing code in a Background Service. and everything works well except the Notification isn't working!

Service code snippet is here:

My Codes occurs inside the TimeDisplayTimerTask inner-class.

This code located at the callback function :

            @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            mIntent = new Intent(getApplicationContext(), MainActivity.class);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0);

            createNotificationChannel();

            Toast.makeText(getApplicationContext(),"A message received : "+ new String(message.getPayload()),Toast.LENGTH_SHORT).show();

            vibrator.vibrate(500);
            myRingtone.play();
            mBuilder .setContentTitle("Message received at : " + mTopic)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setContentText("Message : "+ new String(message.getPayload()));
            mNotificationManager.notify(0, mBuilder.build());
        }

And this code creates a notification channel (as Google developers guide mentioned to write):

Posted with help of this answer.

private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String chanel_id = "3000";
            CharSequence name = "Mqtt message";
            String description = "Message arrived";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.BLUE);
            mNotificationManager = getSystemService(NotificationManager.class);
            if (mNotificationManager != null) {
                mNotificationManager.createNotificationChannel(mChannel);
            }

            mBuilder = new NotificationCompat.Builder(getApplicationContext(), chanel_id);
        }
        else
        {
            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mBuilder = new NotificationCompat.Builder(getApplicationContext());
        }

    }

When it receives a message, A toast message appears holding the message. But it doesn't push notification.

Checked Apps and Notifications, All notifications are allowed.

SOLVED : the setter method .setSmallIcon must be called to successfully build a notification.

Which was not important to me.

Hamza Hajeir
  • 119
  • 1
  • 8
  • Create a simpler code for posting a notification on a button click – Alexander Farber Dec 15 '18 at 09:24
  • Done that, First crashed because it hasn't valid small icon, Added it then notification performed well. Implemented that change into Service java file, Nothing changed! It seems that the code isn't activated yet ! (Thus it should crash before because of not-valid small icon). – Hamza Hajeir Dec 15 '18 at 11:19
  • Your `Toast.makeText(...).show()` might be crashing, because Paho´s `messageArrived()` is not called on the UI thread. Put the logs around this call. – Alexander Farber Dec 15 '18 at 11:52
  • Well, It Works ! `.setSmallIcon` is the solution! It seems to be that i had a false app building last time i tested it. But i still uncertain why app didn't crash as a simple button test. Thanks a lot for your guidance @AlexanderFarber – Hamza Hajeir Dec 15 '18 at 12:51

0 Answers0