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

Android Notification problem app close

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 386 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.
  • DjeffD Offline
    DjeffD Offline
    Djeff
    wrote on 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

    • Login

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