Added notification small icon
@ -31,7 +31,7 @@ int QAndroidNotification::m_InstancesCounter = 0;
|
|||||||
|
|
||||||
QAndroidNotification::QAndroidNotification(QQuickItem *parent) : QQuickItem(parent),
|
QAndroidNotification::QAndroidNotification(QQuickItem *parent) : QQuickItem(parent),
|
||||||
m_JavaNotification("com/falsinsoft/qtandroidtools/AndroidNotification",
|
m_JavaNotification("com/falsinsoft/qtandroidtools/AndroidNotification",
|
||||||
"(Landroid/app/Activity;)V",
|
"(Landroid/app/Activity;I)V",
|
||||||
QtAndroid::androidActivity().object<jobject>(),
|
QtAndroid::androidActivity().object<jobject>(),
|
||||||
m_InstancesCounter),
|
m_InstancesCounter),
|
||||||
m_InstanceIndex(m_InstancesCounter++)
|
m_InstanceIndex(m_InstancesCounter++)
|
||||||
@ -49,6 +49,26 @@ const QMap<int, QAndroidNotification*>& QAndroidNotification::Instances()
|
|||||||
return m_pInstancesMap;
|
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
|
const QString& QAndroidNotification::getChannelName() const
|
||||||
{
|
{
|
||||||
return m_ChannelName;
|
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;
|
QAndroidJniObject AndroidBitmap;
|
||||||
|
|
||||||
if(LargeIcon.isNull() == false)
|
if(LargeIcon.isNull() == false)
|
||||||
@ -87,7 +107,39 @@ void QAndroidNotification::setSourceLargeIcon(const QString &SourceLargeIcon)
|
|||||||
"(Landroid/graphics/Bitmap;)V",
|
"(Landroid/graphics/Bitmap;)V",
|
||||||
AndroidBitmap.object()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,8 @@
|
|||||||
class QAndroidNotification : public QQuickItem
|
class QAndroidNotification : public QQuickItem
|
||||||
{
|
{
|
||||||
Q_PROPERTY(QString channelName READ getChannelName WRITE setChannelName)
|
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
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -39,8 +40,13 @@ public:
|
|||||||
|
|
||||||
const QString& getChannelName() const;
|
const QString& getChannelName() const;
|
||||||
void setChannelName(const QString &ChannelName);
|
void setChannelName(const QString &ChannelName);
|
||||||
const QString& getSourceLargeIcon() const;
|
const QString& getLargeIconSource() const;
|
||||||
void setSourceLargeIcon(const QString &SourceLargeIcon);
|
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();
|
static const QMap<int, QAndroidNotification*>& Instances();
|
||||||
|
|
||||||
@ -50,7 +56,7 @@ private:
|
|||||||
static int m_InstancesCounter;
|
static int m_InstancesCounter;
|
||||||
const int m_InstanceIndex;
|
const int m_InstanceIndex;
|
||||||
QString m_ChannelName;
|
QString m_ChannelName;
|
||||||
QString m_SourceLargeIcon;
|
QString m_LargeIconSource, m_SmallIconName;
|
||||||
|
|
||||||
QAndroidJniObject ImageToAndroidBitmap(const QImage &img);
|
QAndroidJniObject ImageToAndroidBitmap(const QImage &img);
|
||||||
};
|
};
|
||||||
|
@ -31,26 +31,48 @@ import android.os.Build;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
|
import android.support.v4.app.NotificationManagerCompat;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
|
||||||
public class AndroidNotification
|
public class AndroidNotification
|
||||||
{
|
{
|
||||||
private final String NOTIFICATION_CHANNEL_ID;
|
private final String NOTIFICATION_CHANNEL_ID;
|
||||||
private final Activity mActivityInstance;
|
private final Activity mActivityInstance;
|
||||||
|
private final int mNotificationId;
|
||||||
|
|
||||||
|
private int mSmallIconResourceId = 0;
|
||||||
private Bitmap mLargeIcon = null;
|
private Bitmap mLargeIcon = null;
|
||||||
|
|
||||||
public AndroidNotification(Activity ActivityInstance, int InstanceId)
|
public AndroidNotification(Activity ActivityInstance, int InstanceId)
|
||||||
{
|
{
|
||||||
NOTIFICATION_CHANNEL_ID = (ActivityInstance.getClass().getName() + Integer.toString(InstanceId));
|
NOTIFICATION_CHANNEL_ID = (ActivityInstance.getClass().getName() + Integer.toString(InstanceId));
|
||||||
mActivityInstance = ActivityInstance;
|
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)
|
public void setLargeIcon(Bitmap icon)
|
||||||
|
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 537 B |
After Width: | Height: | Size: 534 B |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 2.4 KiB |
After Width: | Height: | Size: 2.3 KiB |
@ -16,13 +16,36 @@ Page {
|
|||||||
QtAndroidNotification {
|
QtAndroidNotification {
|
||||||
id: notification1
|
id: notification1
|
||||||
channelName: "Notification channel 1"
|
channelName: "Notification channel 1"
|
||||||
|
smallIconName: "qtatoolsdemo_notification_icon1"
|
||||||
|
largeIconSource: ":/images/logo_falsinsoft.jpg"
|
||||||
}
|
}
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
text: "Show notification"
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|