2

I want get notification from my app using qt-android, I found this examole in qt examples , it's in QML and I want use it in QWidgets,To use code in QWidget I changed it as follow:

notificationclient.h

#ifndef NOTIFICATIONCLIENT_H
#define NOTIFICATIONCLIENT_H

#include <QObject>

class NotificationClient : public QObject
{
    Q_OBJECT
public:
    explicit NotificationClient(QObject *parent = 0);

    void setNotification(QString notification);
    QString notification() const;

signals:
    void notificationChanged();

private slots:
    void updateAndroidNotification();

private:
    QString m_notification;
};

#endif // NOTIFICATIONCLIENT_H

notificationclient.cpp

#include "notificationclient.h"

#include <QtAndroidExtras/QAndroidJniObject>

NotificationClient::NotificationClient(QObject *parent)
    : QObject(parent)
{
    connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
    m_notification = "";
}

void NotificationClient::setNotification(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/notification/NotificationClient",
                                       "notify",
                                       "(Ljava/lang/String;)V",
                                       javaNotification.object<jstring>());
}

For use in Main class:

notification = new NotificationClient(this);

And for get notification:

void myclass::on_btn_clicked(){
notification->setNotification("hello world");
}

and follow code in .pro file too:

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
QT       += core gui androidextras

when on_btn_clicked() called the program suddenly exits

NOTE:This is the java code and I set package name with my app package

1 Answer 1

2

I solved the problem , we should add this attribute to activity tag in AndroidMainifest.xml

android:name="MY.APP.PACKAGE.NAME.NotificationClient"
Sign up to request clarification or add additional context in comments.

2 Comments

Hello please can you give me the full syntax of it to add to AndroidMainifest.xml
i mean where exactly to add it in AndroidMainifest.xml and how Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.