无法使用Android上的AlarmManager安排通知(使用Qt)

编程入门 行业动态 更新时间:2024-10-22 23:40:49
本文介绍了无法使用Android上的AlarmManager安排通知(使用Qt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在通过qt 5.5执行以下操作。项目。

I am doing the following from a qt 5.5. project.

我正在尝试使用android中的警报管理器计划本地通知。这是安排通知的代码:

I am trying to schedule a local notification using the alarm manger in android. This is the code to schedule the notification:

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,应显示通知:

And this is the NotificationPublisher, which should display the notification:

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); } }

对于调试人,我将唤醒时间设置为0(因此通知应立即显示)。

For debug puproses I set the "wakeup" time to 0 (so the notification should appear immediately).

Lod.d(!,预定)输出出现在控制台输出中,但 Log.d(!, Notified)没有。因此,我是否可以将警报调度为某种方式不正确?

The Lod.d("!","Scheduled") output appears in the console output, but the Log.d("!", "Notified") does not. So am I scheduling the alarm somehow incorrect?

推荐答案

我在AndroidManifest.xml中出错。 NotificationPublisher需要注册为接收者,如下所示:

I had an error in the AndroidManifest.xml. The NotificationPublisher needs to be registered as a receiver, like this:

<receiver android:name="de.goodpoint_heidelberg.NotificationPublisher" android:enabled="true"/>

更多推荐

无法使用Android上的AlarmManager安排通知(使用Qt)

本文发布于:2023-11-27 09:24:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   Android   AlarmManager   Qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!