NotificationClient



  • 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



  • 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



  • 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



  • 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?



  • 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 ^_^ )



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.