我正在 qt 5.5 中执行以下操作。项目。
我正在尝试使用 android 中的警报管理器安排本地通知。这是安排通知的代码:
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,它应该显示通知:
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);
}
}
对于调试 puproses,我将“唤醒”时间设置为 0(因此通知应立即出现)。
The Lod.d("!","Scheduled")
输出出现在控制台输出中,但是Log.d("!", "Notified")
才不是。那么我安排的闹钟是否不正确?