Added notification small icon

This commit is contained in:
FalsinSoft 2019-03-29 16:31:33 +01:00
parent 65b6198684
commit e0027ed34e
14 changed files with 118 additions and 15 deletions

View File

@ -31,7 +31,7 @@ int QAndroidNotification::m_InstancesCounter = 0;
QAndroidNotification::QAndroidNotification(QQuickItem *parent) : QQuickItem(parent),
m_JavaNotification("com/falsinsoft/qtandroidtools/AndroidNotification",
"(Landroid/app/Activity;)V",
"(Landroid/app/Activity;I)V",
QtAndroid::androidActivity().object<jobject>(),
m_InstancesCounter),
m_InstanceIndex(m_InstancesCounter++)
@ -49,6 +49,26 @@ const QMap<int, QAndroidNotification*>& QAndroidNotification::Instances()
return m_pInstancesMap;
}
void QAndroidNotification::show(const QString &title, const QString &content)
{
if(m_JavaNotification.isValid() && m_SmallIconName.isEmpty() == false)
{
m_JavaNotification.callMethod<void>("show",
"(Ljava/lang/String;Ljava/lang/String;)V",
QAndroidJniObject::fromString(title).object<jstring>(),
QAndroidJniObject::fromString(content).object<jstring>()
);
}
}
void QAndroidNotification::cancel()
{
if(m_JavaNotification.isValid())
{
m_JavaNotification.callMethod<void>("cancel");
}
}
const QString& QAndroidNotification::getChannelName() const
{
return m_ChannelName;
@ -66,14 +86,14 @@ void QAndroidNotification::setChannelName(const QString &ChannelName)
}
}
const QString& QAndroidNotification::getSourceLargeIcon() const
const QString& QAndroidNotification::getLargeIconSource() const
{
return m_SourceLargeIcon;
return m_LargeIconSource;
}
void QAndroidNotification::setSourceLargeIcon(const QString &SourceLargeIcon)
void QAndroidNotification::setLargeIconSource(const QString &LargeIconSource)
{
const QImage LargeIcon(SourceLargeIcon);
const QImage LargeIcon((LargeIconSource.length() > 5 && LargeIconSource.left(5) == "qrc:/") ? LargeIconSource.right(LargeIconSource.length() - 3) : LargeIconSource);
QAndroidJniObject AndroidBitmap;
if(LargeIcon.isNull() == false)
@ -87,7 +107,39 @@ void QAndroidNotification::setSourceLargeIcon(const QString &SourceLargeIcon)
"(Landroid/graphics/Bitmap;)V",
AndroidBitmap.object()
);
m_SourceLargeIcon = SourceLargeIcon;
m_LargeIconSource = LargeIconSource;
}
}
const QString& QAndroidNotification::getSmallIconName() const
{
return m_SmallIconName;
}
void QAndroidNotification::setSmallIconName(const QString &SmallIconName)
{
const QAndroidJniObject Activity = QtAndroid::androidActivity();
QAndroidJniObject PackageName, PackageManager, Resources;
int ResourceId;
PackageManager = Activity.callObjectMethod("getPackageManager", "()Landroid/content/pm/PackageManager;");
PackageName = Activity.callObjectMethod("getPackageName", "()Ljava/lang/String;");
Resources = Activity.callObjectMethod("getResources", "()Landroid/content/res/Resources;");
ResourceId = Resources.callMethod<jint>("getIdentifier",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
QAndroidJniObject::fromString(SmallIconName).object<jstring>(),
QAndroidJniObject::fromString("drawable").object<jstring>(),
PackageName.object<jstring>()
);
if(m_JavaNotification.isValid() && ResourceId != 0)
{
m_JavaNotification.callMethod<void>("setSmallIconResourceId",
"(I)V",
ResourceId
);
m_SmallIconName = SmallIconName;
}
}

View File

@ -30,7 +30,8 @@
class QAndroidNotification : public QQuickItem
{
Q_PROPERTY(QString channelName READ getChannelName WRITE setChannelName)
Q_PROPERTY(QString sourceLargeIcon READ getSourceLargeIcon WRITE setSourceLargeIcon)
Q_PROPERTY(QString largeIconSource READ getLargeIconSource WRITE setLargeIconSource)
Q_PROPERTY(QString smallIconName READ getSmallIconName WRITE setSmallIconName)
Q_OBJECT
public:
@ -39,8 +40,13 @@ public:
const QString& getChannelName() const;
void setChannelName(const QString &ChannelName);
const QString& getSourceLargeIcon() const;
void setSourceLargeIcon(const QString &SourceLargeIcon);
const QString& getLargeIconSource() const;
void setLargeIconSource(const QString &LargeIconSource);
const QString& getSmallIconName() const;
void setSmallIconName(const QString &SmallIconName);
Q_INVOKABLE void show(const QString &title, const QString &content);
Q_INVOKABLE void cancel();
static const QMap<int, QAndroidNotification*>& Instances();
@ -50,7 +56,7 @@ private:
static int m_InstancesCounter;
const int m_InstanceIndex;
QString m_ChannelName;
QString m_SourceLargeIcon;
QString m_LargeIconSource, m_SmallIconName;
QAndroidJniObject ImageToAndroidBitmap(const QImage &img);
};

View File

@ -31,26 +31,48 @@ import android.os.Build;
import android.graphics.Bitmap;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
public class AndroidNotification
{
private final String NOTIFICATION_CHANNEL_ID;
private final Activity mActivityInstance;
private final int mNotificationId;
private int mSmallIconResourceId = 0;
private Bitmap mLargeIcon = null;
public AndroidNotification(Activity ActivityInstance, int InstanceId)
{
NOTIFICATION_CHANNEL_ID = (ActivityInstance.getClass().getName() + Integer.toString(InstanceId));
mActivityInstance = ActivityInstance;
mNotificationId = InstanceId;
}
public void show(String content)
public void show(String title, String content)
{
NotificationCompat.Builder notification = new NotificationCompat.Builder(mActivityInstance, NOTIFICATION_CHANNEL_ID);
NotificationCompat.Builder AppNotification = new NotificationCompat.Builder(mActivityInstance, NOTIFICATION_CHANNEL_ID);
NotificationManagerCompat Manager = NotificationManagerCompat.from(mActivityInstance);
if(mLargeIcon != null) notification.setLargeIcon(mLargeIcon);
AppNotification.setSmallIcon(mSmallIconResourceId);
AppNotification.setContentTitle(title);
AppNotification.setContentText(content);
AppNotification.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if(mLargeIcon != null) AppNotification.setLargeIcon(mLargeIcon);
Manager.notify(mNotificationId, AppNotification.build());
}
public void cancel()
{
NotificationManagerCompat Manager = NotificationManagerCompat.from(mActivityInstance);
Manager.cancel(mNotificationId);
}
public void setSmallIconResourceId(int iconId)
{
mSmallIconResourceId = iconId;
}
public void setLargeIcon(Bitmap icon)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -16,13 +16,36 @@ Page {
QtAndroidNotification {
id: notification1
channelName: "Notification channel 1"
smallIconName: "qtatoolsdemo_notification_icon1"
largeIconSource: ":/images/logo_falsinsoft.jpg"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Show notification"
onClicked: {
}
onClicked: notification1.show("test title 1", "test content")
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Cancel notification"
onClicked: notification1.cancel()
}
QtAndroidNotification {
id: notification2
channelName: "Notification channel 2"
smallIconName: "qtatoolsdemo_notification_icon2"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Show notification"
onClicked: notification2.show("test title 2", "test content")
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Cancel notification"
onClicked: notification2.cancel()
}
}
}