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. Android notification
Forum Updated to NodeBB v4.3 + New Features

Android notification

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 3 Posters 1.3k 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.
  • EndrII 0E Offline
    EndrII 0E Offline
    EndrII 0
    wrote on last edited by
    #1

    Hi
    I want to add notifications to my application written to qml.

    I studied the example and created the same for myself, the application is successfully compiled, but when I try to show the notification it falls.

    Here is the console output:

    E/MediaPlayer(24986): Should have subtitle controller already set
    F/art     (24986): art/runtime/check_jni.cc:70] JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception 'java.lang.NullPointerException' thrown in unknown throw location
    F/art     (24986): art/runtime/check_jni.cc:70]     in call to GetMethodID
    F/art     (24986): art/runtime/check_jni.cc:70]     from void org.qtproject.qt5.android.QtNative.startQtApplication()
    F/art     (24986): art/runtime/check_jni.cc:70] "qtMainLoopThread" prio=5 tid=18 Runnable
    F/art     (24986): art/runtime/check_jni.cc:70]   | group="main" sCount=0 dsCount=0 obj=0x12e5d460 self=0xf46bac00
    F/art     (24986): art/runtime/check_jni.cc:70]   | sysTid=25015 nice=0 cgrp=default sched=0/0 handle=0xf3012000
    F/art     (24986): art/runtime/check_jni.cc:70]   | state=R schedstat=( 1749781057 405706930 1629 ) utm=164 stm=10 core=0 HZ=100
    F/art     (24986): art/runtime/check_jni.cc:70]   | stack=0xe0afe000-0xe0b00000 stackSize=1036KB
    F/art     (24986): art/runtime/check_jni.cc:70]   | held mutexes= "mutator lock"(shared held)
    F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtNative.startQtApplication(Native method)
    F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:359)
    F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)
    F/art     (24986): art/runtime/check_jni.cc:70]   at java.lang.Thread.run(Thread.java:818)
    F/art     (24986): art/runtime/check_jni.cc:70]
    W/InputMethodManagerService(  860): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@2a70763 (uid=10136 pid=24986)
    W/InputMethodManager(24986): startInputInner : InputBindResult == null
    F/libc    (24986): Fatal signal 6 (SIGABRT), code -6 in tid 25015 (qtMainLoopThrea)
    I/AEE/AED (25314): check process 24986 name:sarapp.sounband
    I/AEE/AED (25314): tid 25015 abort msg address is:0xdc1c5000, si_code is:-6 (request from 24986:10136)
    I/AEE/AED (25314): BOOM: pid=24986 uid=10136 gid=10136 tid=25015
    I/ActivityManager(  860): Process org.quasarapp.sounband (pid 24986) has died
    I/SurfaceFlinger(  302): EventThread Client Pid (24986) disconnected by (302)
    I/SurfaceFlinger(  302): EventThread Client Pid (24986) disconnected by (302)
    W/ADB_SERVICES(20741): terminating JDWP 24986 connection: Try again
    I/Zygote  (  363): Process 24986 exited due to signal (6)
    

    java code:

    package org.quasarapp.sounband;
    
    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.setSmallIcon(R.drawable.icon);
                m_builder.setContentTitle("A message from SoundBand!");
            }
    
            m_builder.setContentText(s);
            m_notificationManager.notify(1, m_builder.build());
        }
    }
    
    
    

    c++ header code

    
    #ifndef ANDROIDPLAYER_H
    #define ANDROIDPLAYER_H
    
    #include <QObject>
    #ifdef Q_OS_ANDROID
    
    class AndroidPlayer : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged)
    
    private:
        QString m_notification;
    
    private slots:
        void updateAndroidNotification();
    
    public:
        explicit AndroidPlayer(QObject *parent = nullptr);
    
    QString notification() const;
    
    public slots:
        void setNotification(QString notification);
    
    signals:
        void notificationChanged();
    };
    
    #endif // OS_ANDROID
    
    #endif // ANDROIDPLAYER_H
    
    
    

    c++ source code

    #include "androidplayer.h"
    #ifdef Q_OS_ANDROID
    
    #include <QAndroidJniObject>
    
    AndroidPlayer::AndroidPlayer(QObject *parent)
        : QObject(parent) {
    
        connect(this, SIGNAL(notificationChanged()),
                this, SLOT(updateAndroidNotification()));
    }
    
    QString AndroidPlayer::notification() const {
        return m_notification;
    }
    
    void AndroidPlayer::setNotification(QString notification) {
        if (m_notification == notification)
            return;
    
        m_notification = notification;
        emit notificationChanged();
    }
    
    void AndroidPlayer::updateAndroidNotification() {
        QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
        QAndroidJniObject::callStaticMethod<void>("org.quasarapp.sounband.NotificationClient",
                                           "notify",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    }
    
    #endif
    
    
    KroMignonK 1 Reply Last reply
    0
    • A Offline
      A Offline
      adddeeee
      wrote on last edited by
      #2

      Hi!

      I'm having the exact same problem. Did you solve this?

      KroMignonK 1 Reply Last reply
      0
      • EndrII 0E EndrII 0

        Hi
        I want to add notifications to my application written to qml.

        I studied the example and created the same for myself, the application is successfully compiled, but when I try to show the notification it falls.

        Here is the console output:

        E/MediaPlayer(24986): Should have subtitle controller already set
        F/art     (24986): art/runtime/check_jni.cc:70] JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception 'java.lang.NullPointerException' thrown in unknown throw location
        F/art     (24986): art/runtime/check_jni.cc:70]     in call to GetMethodID
        F/art     (24986): art/runtime/check_jni.cc:70]     from void org.qtproject.qt5.android.QtNative.startQtApplication()
        F/art     (24986): art/runtime/check_jni.cc:70] "qtMainLoopThread" prio=5 tid=18 Runnable
        F/art     (24986): art/runtime/check_jni.cc:70]   | group="main" sCount=0 dsCount=0 obj=0x12e5d460 self=0xf46bac00
        F/art     (24986): art/runtime/check_jni.cc:70]   | sysTid=25015 nice=0 cgrp=default sched=0/0 handle=0xf3012000
        F/art     (24986): art/runtime/check_jni.cc:70]   | state=R schedstat=( 1749781057 405706930 1629 ) utm=164 stm=10 core=0 HZ=100
        F/art     (24986): art/runtime/check_jni.cc:70]   | stack=0xe0afe000-0xe0b00000 stackSize=1036KB
        F/art     (24986): art/runtime/check_jni.cc:70]   | held mutexes= "mutator lock"(shared held)
        F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtNative.startQtApplication(Native method)
        F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:359)
        F/art     (24986): art/runtime/check_jni.cc:70]   at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)
        F/art     (24986): art/runtime/check_jni.cc:70]   at java.lang.Thread.run(Thread.java:818)
        F/art     (24986): art/runtime/check_jni.cc:70]
        W/InputMethodManagerService(  860): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@2a70763 (uid=10136 pid=24986)
        W/InputMethodManager(24986): startInputInner : InputBindResult == null
        F/libc    (24986): Fatal signal 6 (SIGABRT), code -6 in tid 25015 (qtMainLoopThrea)
        I/AEE/AED (25314): check process 24986 name:sarapp.sounband
        I/AEE/AED (25314): tid 25015 abort msg address is:0xdc1c5000, si_code is:-6 (request from 24986:10136)
        I/AEE/AED (25314): BOOM: pid=24986 uid=10136 gid=10136 tid=25015
        I/ActivityManager(  860): Process org.quasarapp.sounband (pid 24986) has died
        I/SurfaceFlinger(  302): EventThread Client Pid (24986) disconnected by (302)
        I/SurfaceFlinger(  302): EventThread Client Pid (24986) disconnected by (302)
        W/ADB_SERVICES(20741): terminating JDWP 24986 connection: Try again
        I/Zygote  (  363): Process 24986 exited due to signal (6)
        

        java code:

        package org.quasarapp.sounband;
        
        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.setSmallIcon(R.drawable.icon);
                    m_builder.setContentTitle("A message from SoundBand!");
                }
        
                m_builder.setContentText(s);
                m_notificationManager.notify(1, m_builder.build());
            }
        }
        
        
        

        c++ header code

        
        #ifndef ANDROIDPLAYER_H
        #define ANDROIDPLAYER_H
        
        #include <QObject>
        #ifdef Q_OS_ANDROID
        
        class AndroidPlayer : public QObject
        {
            Q_OBJECT
            Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged)
        
        private:
            QString m_notification;
        
        private slots:
            void updateAndroidNotification();
        
        public:
            explicit AndroidPlayer(QObject *parent = nullptr);
        
        QString notification() const;
        
        public slots:
            void setNotification(QString notification);
        
        signals:
            void notificationChanged();
        };
        
        #endif // OS_ANDROID
        
        #endif // ANDROIDPLAYER_H
        
        
        

        c++ source code

        #include "androidplayer.h"
        #ifdef Q_OS_ANDROID
        
        #include <QAndroidJniObject>
        
        AndroidPlayer::AndroidPlayer(QObject *parent)
            : QObject(parent) {
        
            connect(this, SIGNAL(notificationChanged()),
                    this, SLOT(updateAndroidNotification()));
        }
        
        QString AndroidPlayer::notification() const {
            return m_notification;
        }
        
        void AndroidPlayer::setNotification(QString notification) {
            if (m_notification == notification)
                return;
        
            m_notification = notification;
            emit notificationChanged();
        }
        
        void AndroidPlayer::updateAndroidNotification() {
            QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
            QAndroidJniObject::callStaticMethod<void>("org.quasarapp.sounband.NotificationClient",
                                               "notify",
                                               "(Ljava/lang/String;)V",
                                               javaNotification.object<jstring>());
        }
        
        #endif
        
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @EndrII-0 I don't know if it will change anything, but I would do it like this, to ensure it will run on main thread:

        void AndroidPlayer::setNotification(QString notification) {
            if (m_notification == notification)
                return;
        
            m_notification = notification;
            QtAndroid::runOnAndroidThread([notification] {
                auto javaString = QAndroidJniObject::fromString(notification);
                QAndroidJniObject::callStaticMethod<void>("org.quasarapp.sounband.NotificationClient",
                                                   "notify",
                                                   "(Ljava/lang/String;)V",
                                                   javaString.object());        
            });
        }
        

        Or if you only want to show a toast notification:

        void AndroidPlayer::showToast(const QString &message, bool shortTime)
        {
            const int duration(shortTime ? 0 : 1);
            QtAndroid::runOnAndroidThread([message, duration] {
                auto javaString = QAndroidJniObject::fromString(message);
                auto 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");
            });
        
        }
        
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • A adddeeee

          Hi!

          I'm having the exact same problem. Did you solve this?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @adddeeee @EndrII-0 , there is an open source (MIT licence) on github which could help you to solve your problem.
          Take a look at QtAndroidTools

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adddeeee
            wrote on last edited by
            #5

            Thanks! I think I have that code implemented.

            I'm starting to think something's wrong with where I place the icon itself.

            Where do you place the icon?

            KroMignonK 1 Reply Last reply
            0
            • A adddeeee

              Thanks! I think I have that code implemented.

              I'm starting to think something's wrong with where I place the icon itself.

              Where do you place the icon?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @adddeeee I never used Notification on Android, so I can't reply to your question.
              I don't think you are using same code a QtAndroidTools, because there is a C++ wrapper around all Android/JNI stuff which seems to made it quiet simple to use.. Take a look at the documentation: https://falsinsoft.github.io/QtAndroidTools/Documentation/#Notification

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              A 1 Reply Last reply
              1
              • KroMignonK KroMignon

                @adddeeee I never used Notification on Android, so I can't reply to your question.
                I don't think you are using same code a QtAndroidTools, because there is a C++ wrapper around all Android/JNI stuff which seems to made it quiet simple to use.. Take a look at the documentation: https://falsinsoft.github.io/QtAndroidTools/Documentation/#Notification

                A Offline
                A Offline
                adddeeee
                wrote on last edited by adddeeee
                #7

                @KroMignon said in Android notification:

                @adddeeee I never used Notification on Android, so I can't reply to your question.
                I don't think you are using same code a QtAndroidTools, because there is a C++ wrapper around all Android/JNI stuff which seems to made it quiet simple to use.. Take a look at the documentation:

                No, that's true. I'm using the same code as in the Qt Notifier example.

                Please have a look in this thread where I'm posting my code:

                https://forum.qt.io/topic/103921/android-notification-not-shown/7

                1 Reply Last reply
                0

                • Login

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