Unsolved Android notification
-
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
-
Hi!
I'm having the exact same problem. Did you solve this?
-
@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"); }); }
-
@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 -
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?
-
@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 -
@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