无法使用Android上的AlarmManager安排通知(使用Qt) [英] Unable to schedule notification with AlarmManager on Android (using Qt)

查看:94
本文介绍了无法使用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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆