Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Android Notification problem app close
Forum Update on Monday, May 27th 2025

Android Notification problem app close

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 379 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.
  • D Offline
    D Offline
    Djeff
    wrote on 24 Nov 2016, 16:43 last edited by Djeff
    #1

    I'm currently trying to set some notification using Qt 5.7 for Android. The notification work fine when the App is open and when the App is on the background, but the notification stop working when Android clear the RAM.

    Here my code, notification.cpp

    #include "notificationclient.h"
    #include <QDebug>
    
    #include <QtAndroidExtras/QAndroidJniObject>
    
    NotificationClient::NotificationClient(QObject *parent)
        : QObject(parent)
    {
        connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
    }
    
    void NotificationClient::setNotification(const 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>("com/loayza/medit/NotificationClient",
                                           "notify",
                                           "(Ljava/lang/String;)V",
                                           javaNotification.object<jstring>());
    }
    

    notification.java :

    package com.loayza.medit;
    
    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.iconnotif);
                m_builder.setContentTitle("Tratamiento !");
            }
    
            m_builder.setContentText(s);
    
            m_notificationManager.notify(1, m_builder.build());
        }
    }
    

    What can I do to keep my App running on the background even after Android close it ?

    Thanks for your help

    1 Reply Last reply
    0

    1/1

    24 Nov 2016, 16:43

    • Login

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