Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to make android notification using c++

How to make android notification using c++

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmrCoder
    wrote on 19 Nov 2016, 18:35 last edited by SGaist
    #1

    I want to make an android notification so i link a java class to use QAndroidJni to make that
    java class

    package org.qtproject.example;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;
    
    public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
    {
        private static NotificationManager m_notificationManager;
        private static Notification.Builder m_builder;
        private static NotificationClient m_instance;
    
        public NotificationClient()
        {
            m_instance = this;
        }
    
        public static void notify(String s)
        {
            if (m_notificationManager == null) {
                m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
                m_builder = new Notification.Builder(m_instance);
                m_builder.setContentTitle("A message from Qt!");
            }
    
            m_builder.setContentText(s);
            m_notificationManager.notify(1, m_builder.build());
        }
    }
    
    

    I got this java class from an example in Qt but it used with QML
    i link this to my .pro file

    
    QT       += core gui androidextras
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = TestNofi
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            mainwindow.cpp \
        notificationclient.cpp
    
    HEADERS  += mainwindow.h \
        notificationclient.h
    
    FORMS    += mainwindow.ui
    
    CONFIG += mobility
    MOBILITY = 
    
    DISTFILES += \
        android/AndroidManifest.xml \
        android/gradle/wrapper/gradle-wrapper.jar \
        android/gradlew \
        android/res/values/libs.xml \
        android/build.gradle \
        android/gradle/wrapper/gradle-wrapper.properties \
        android/gradlew.bat \
        android/src/org/qtproject/example/NotificationClient.java // here added
    
    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
    
    
    
    

    i add this class
    .h

    #ifndef NOTIFICATIONCLIENT_H
    #define NOTIFICATIONCLIENT_H
    
    #include <QObject>
    
    class NotificationClient : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged)
    public:
        explicit NotificationClient(QObject *parent = 0);
    
        void setNotification(const QString &notification);
        QString notification() const;
    
    signals:
        void notificationChanged();
    
    private slots:
        void updateAndroidNotification();
    
    private:
        QString m_notification;
    };
    
    #endif // NOTIFICATIONCLIENT_H
    
    

    .cpp

    #include "notificationclient.h"
    
    #include <QtAndroidExtras/QAndroidJniObject>
    
    NotificationClient::NotificationClient(QObject *parent)
        : QObject(parent)
    {
        connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
    }
    
    void NotificationClient::setNotification(const QString &notification)
    {
        if (m_notification == notification)
            return;
    
        m_notification = notification;
        emit notificationChanged();
    }
    
    QString NotificationClient::notification() const
    {
        return m_notification;
    }
    
    void NotificationClient::updateAndroidNotification()
    {
        QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
        QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/NotificationClient",
                                           "notify",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    }
    
    

    i use it in my mainwindow like that

    void MainWindow::on_pushButton_clicked()
    {
        qDebug() << "Is Class Avlibale = " << QAndroidJniObject::isClassAvailable("org/qtproject/example/NotificationClient"); // return true
        client->setNotification("Hello world Android Nofication");
    }
    

    but when deploy it on my android phone i don't get any notification that is what i try.
    so what should i do to make a notification using c++ i hope there something easy as i make a toast using qt 5.7 no need to use a java class like this

    void showToast(const QString &message, Duration duration) {
        // all the magic must happen on Android UI thread
        QtAndroid::runOnAndroidThread([message, duration] {
            QAndroidJniObject javaString = QAndroidJniObject::fromString(message);
            QAndroidJniObject toast = QAndroidJniObject::callStaticObjectMethod("android/widget/Toast", "makeText",
                                                                                "(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;",
                                                                                QtAndroid::androidActivity().object(),
                                                                                javaString.object(),
                                                                                jint(duration));
            toast.callMethod<void>("show");
        });
    }
    

    or any other way to do that
    Thanks in Advance

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AmrCoder
      wrote on 20 Nov 2016, 22:10 last edited by AmrCoder
      #2

      finally, I found the solution is to add the package/Java class
      in the activity section in android manifest .xml file and no need to use the org.qtproject.qt5.android.bindings.QtActivity because the notification file already extends this class
      so change android:name in activity section in android .xml
      with the java class (pakacge.class.java)
      this what I understand and works with me I hope it helps

      1 Reply Last reply
      0

      1/2

      19 Nov 2016, 18:35

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved