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

NotificationClient

Scheduled Pinned Locked Moved Mobile and Embedded
androidqt 5.4.1
6 Posts 5 Posters 2.2k Views 2 Watching
  • 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.
  • S Offline
    S Offline
    shedo
    wrote on last edited by
    #1

    Hi all,
    I'm trying to implement my NotificationClient and for test I added the code of Qt example (notifier). The only difference is on the package in the java code.
    The cpp class calls the java static method (public static void notify(String s)) but it returns a null pointer exception because m_instance in not initialized. So, I added some print and I noticed that the constructor of the java code (public NotificationClient()) was never called in my project instead in the Qt example was called. I searched in the Qt example how and who call the constructor but I had no luck.

    This is my java code:

    package it.edalab.boxiovisualizer;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.widget.Toast;
    
    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;
        private static Context context;
    
        public NotificationClient()
        {
            System.out.println("Constructor");
            m_instance = this;
        }
    
        public static void notify(String s)
        {
            //Toast.makeText(context, s, Toast.LENGTH_LONG).show();
            try {
                System.out.println("value:"+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 Qt!");
                }
    
                m_builder.setContentText(s);
                m_notificationManager.notify(1, m_builder.build());
    
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    The cpp and .h are the same of qt example except for:

    void NotificationClient::updateAndroidNotification()
    {
        qDebug() << "notification";
        QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
        QAndroidJniObject::callStaticMethod<void>("it/edalab/boxiovisualizer/NotificationClient",
                                           "notify",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    
    }
    
    

    Can you explain how and who calls the constructor?

    Thanks all,
    shedo

    R 1 Reply Last reply
    0
    • S shedo

      Hi all,
      I'm trying to implement my NotificationClient and for test I added the code of Qt example (notifier). The only difference is on the package in the java code.
      The cpp class calls the java static method (public static void notify(String s)) but it returns a null pointer exception because m_instance in not initialized. So, I added some print and I noticed that the constructor of the java code (public NotificationClient()) was never called in my project instead in the Qt example was called. I searched in the Qt example how and who call the constructor but I had no luck.

      This is my java code:

      package it.edalab.boxiovisualizer;
      
      import android.app.Notification;
      import android.app.NotificationManager;
      import android.content.Context;
      import android.widget.Toast;
      
      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;
          private static Context context;
      
          public NotificationClient()
          {
              System.out.println("Constructor");
              m_instance = this;
          }
      
          public static void notify(String s)
          {
              //Toast.makeText(context, s, Toast.LENGTH_LONG).show();
              try {
                  System.out.println("value:"+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 Qt!");
                  }
      
                  m_builder.setContentText(s);
                  m_notificationManager.notify(1, m_builder.build());
      
              } catch (Exception e){
                  e.printStackTrace();
              }
          }
      }
      

      The cpp and .h are the same of qt example except for:

      void NotificationClient::updateAndroidNotification()
      {
          qDebug() << "notification";
          QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
          QAndroidJniObject::callStaticMethod<void>("it/edalab/boxiovisualizer/NotificationClient",
                                             "notify",
                                             "(Ljava/lang/String;)V",
                                             javaNotification.object<jstring>());
      
      }
      
      

      Can you explain how and who calls the constructor?

      Thanks all,
      shedo

      R Offline
      R Offline
      ReinaldoJr
      wrote on last edited by
      #2

      Hello,

      Apparently the Java code is OK.
      How is the project name? An interesting point is that the name of the java class seve be equal to the project name, along with the AndroidManifest.xml settings

      Assuming your project is named 'NotificationClient' then:

      -> in NotificationClient.java
      org.qtproject package;
      public class NotificationClient {...}

      -> in AndroidManifest.xml
      =>in tag manifest
      package = "org.qtproject"
      =>int tag activity
      android: name = "org.qtproject.NotificationClient"

      -> in root path folders and files
      android/AndroidManifest.xml
      android/src/org/qtproject/NotificationClient.java

      -> in NotificationClient.pro
      ANDROID_PACKAGE_SOURCE_DIR = $$ PWD/android
      OTHER_FILES + =
      android/AndroidManifest.xml
      android/src/org/qtproject/NotificationClient.java

      Cheers

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shedo
        wrote on last edited by
        #3

        Hi,
        I found the problem, I forgot to change Activity android:name in the manifest:

        <activity android:name="it.edalab.boxiovisualizer.NotificationClient" ...
        

        Thanks for the help,
        Ivan

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #4

          I have a similar problem. If I change the android manifest from
          android:name="org.qtproject.qt5.android.bindings.QtActivity"
          to
          android:name="it.denisgottardello.multimediapet.multimediapettouch"
          the program does not start.
          Can you suggest my how I have to write my android manifest?

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          0
          • MrAhmedSayedAliM Offline
            MrAhmedSayedAliM Offline
            MrAhmedSayedAli
            wrote on last edited by
            #5

            i try it but app sent Notification one time and when i click on Notification it not open my app
            how can i fix it ( sorry 4 my bad english ^_^ )

            A 1 Reply Last reply
            0
            • MrAhmedSayedAliM MrAhmedSayedAli

              i try it but app sent Notification one time and when i click on Notification it not open my app
              how can i fix it ( sorry 4 my bad english ^_^ )

              A Offline
              A Offline
              alexvplay
              wrote on last edited by
              #6

              @MrAhmedSayedAli , you would have to set the contentintent on your created notification object in the Java code and then handle the notification according to your needs. As an alternative you might also want to have a look at our plugin which already implements the logic without further effort: http://plugins.v-play.net/plugins/notifications/

              Cheers,
              Alex

              CoFounder of Felgo - https://felgo.com/qt

              Felgo simplifies Mobile App & Game Development with Qt
              What others say: Felgo scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

              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