我正在尝试使用Qt为Android应用创建本地计划的通知。我正在遵循以下答案:Unable to schedule notification with AlarmManager on Android (using Qt),但我无法使其正常运行。
下面是代码 notificationclient.h:
#ifndef NOTIFICATIONCLIENT_H
#define NOTIFICATIONCLIENT_H
#include <QObject>
class NotificationClient : public QObject
{
explicit NotificationClient(QObject *parent = 0);
int createNotification(Qstring title, Qstring content);
};
#endif // NOTIFICATIONCLIENT_H
notificationclient.cpp
#include "notificationclient.h"
#include <QtAndroidExtras/QAndroidJniObject>
NotificationClient::NotificationClient(QObject *parent)
: QObject(parent)
{
}
int NotificationClient::createNotification(Qstring title, Qstring content);
{
QAndroidJniObject javaNotification = QAndroidJniObject;
QAndroidJniObject::callStaticMethod<void>("ScheduledNotifications",
"createNotification",
"(Ljava/lang/String;)V",
javaNotification.object<jstring>());
}
ScheduleNotification.java
class ScheduledNotifications {
static public int notification_id = 0;
static int scheduleNotification(String title, String content, int futureInMilliseconds) {
++notification_id;
Intent notificationIntent = new Intent(QtNative.activity(), NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notification_id);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, createNotification(title,content));
PendingIntent pendingIntent = PendingIntent.getBroadcast(QtNative.activity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)QtNative.activity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, /*futureInMilliseconds*/0, pendingIntent);
Log.d("!" ,"Scheduled");
return notification_id;
}
static public Notification createNotification(String title, String content) {
Notification.Builder builder = new Notification.Builder(QtNative.activity());
builder.setContentTitle(title);
builder.setContentText(content);
return builder.build();
}
}
NotificationPublisher.java
class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {//Called when its time to show the notification ...
Log.d("!", "Notified");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
我不太确定该怎么做。如果有人可以在Qt中提供带有本地通知的适当示例,那将真的很有帮助
谢谢!