I do it the same way as it is done in the example, with one change... adding in the 'try...catch' section just to try to figure out what's going on. It does the same whether or not that is in the code.

I call in my main.cpp:
Qt Code:
  1. NotificationClient *notificationClient = new NotificationClient();
To copy to clipboard, switch view to plain text mode 

...and add it to my QML files so they have access:
Qt Code:
  1. myFunctions->engine->rootContext()->setContextProperty("_notifier", notificationClient);
To copy to clipboard, switch view to plain text mode 

...and call it from QML:
Qt Code:
  1. _notifier.setMyNotification(myNote);
To copy to clipboard, switch view to plain text mode 

notificationclient.h:
Qt Code:
  1. #ifndef NOTIFICATIONCLIENT_H
  2. #define NOTIFICATIONCLIENT_H
  3.  
  4. #include <QObject>
  5.  
  6. class NotificationClient : public QObject
  7. {
  8. Q_OBJECT
  9. Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged);
  10.  
  11. public:
  12. explicit NotificationClient(QObject *parent = 0);
  13.  
  14. void setNotification(const QString &notification);
  15. QString notification() const;
  16.  
  17. public slots:
  18. void setMyNotification(QString myNotification);
  19.  
  20. signals:
  21. void notificationChanged();
  22.  
  23. private slots:
  24. void updateAndroidNotification();
  25.  
  26. private:
  27. QString m_notification;
  28. };
  29.  
  30. #endif // NOTIFICATIONCLIENT_H
To copy to clipboard, switch view to plain text mode 

notificationclient.cpp:
Qt Code:
  1. #include "notificationclient.h"
  2.  
  3. #include <QAndroidJniObject>
  4.  
  5. NotificationClient::NotificationClient(QObject *parent)
  6. : QObject(parent)
  7. {
  8. connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
  9. }
  10.  
  11. void NotificationClient::setNotification(const QString &notification)
  12. {
  13. if (m_notification == notification)
  14. return;
  15.  
  16. m_notification = notification;
  17. emit notificationChanged();
  18. }
  19.  
  20. void NotificationClient::setMyNotification(QString myNotification)
  21. {
  22. setNotification(myNotification);
  23. }
  24.  
  25. QString NotificationClient::notification() const
  26. {
  27. return m_notification;
  28. }
  29.  
  30. void NotificationClient::updateAndroidNotification()
  31. {
  32. QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
  33. QAndroidJniObject::callStaticMethod<void>("com/nascg/nascg/btstWxUpload/NotificationClient",
  34. "notify",
  35. "(Ljava/lang/String;)V",
  36. javaNotification.object<jstring>());
  37. }
To copy to clipboard, switch view to plain text mode 

JAVA:
Qt Code:
  1. package MySuperCustomPackageName;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.content.Context;
  6.  
  7. public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
  8. {
  9. private static NotificationManager m_notificationManager;
  10. private static Notification.Builder m_builder;
  11. private static NotificationClient m_instance;
  12.  
  13. public NotificationClient()
  14. {
  15. m_instance = this;
  16. }
  17.  
  18. public static void notify(String s)
  19. {
  20. try {
  21. if (m_notificationManager == null) {
  22. m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
  23. m_builder = new Notification.Builder(m_instance);
  24. m_builder.setSmallIcon(R.drawable.icon);
  25. m_builder.setContentTitle("A message from Qt!");
  26. }
  27.  
  28. m_builder.setContentText(s);
  29. m_notificationManager.notify(1, m_builder.build());
  30. } catch(Throwable e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
To copy to clipboard, switch view to plain text mode