简体   繁体   English

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

[英]Unable to schedule notification with AlarmManager on Android (using Qt)

I am doing the following from a qt 5.5. 我从qt 5.5做了以下的事情。 project. 项目。

I am trying to schedule a local notification using the alarm manger in android. 我正在尝试使用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();
    }
}

And this is the NotificationPublisher, which should display the notification: 这是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);

    }
}

For debug puproses I set the "wakeup" time to 0 (so the notification should appear immediately). 对于调试puproses我将“唤醒”时间设置为0(因此通知应立即显示)。

The Lod.d("!","Scheduled") output appears in the console output, but the Log.d("!", "Notified") does not. Lod.d("!","Scheduled")输出出现在控制台输出中,但Log.d("!", "Notified")不出现。 So am I scheduling the alarm somehow incorrect? 所以我安排警报不知何故不正确?

Honestly not 100 percent sure why yours is not working but I suspect it is the 0 being passed in. I would think that this should be System.currentTimeInMillis() + SOME_SHORT_INTERVAL ? 老实说,不是100%肯定你为什么不工作,但我怀疑它是0传入。我认为这应该是System.currentTimeInMillis()+ SOME_SHORT_INTERVAL?

This is a working example of an Alarm Manager setup. 这是Alarm Manager设置的工作示例。 Yeah I know its different, but it is something to compare 是的,我知道它的不同,但它是可以比较的东西

Intent syncIntent = new Intent(this, SyncIntentService.class);
       syncIntent.putExtra(EXTRA_REQUEST_KEY,   
       SyncRequestTypes.REQUEST_BACKGROUND_SYNC.name());

    PendingIntent pi = PendingIntent.getService(this, 0, syncIntent,
      Intent.FLAG_ACTIVITY_NO_USER_ACTION);

    // Register first run and then interval for repeated cycles.


   alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + DEFAULT_INITIAL_RUN_TEST,
            DEFAULT_RUN_INTERVAL_TEST, pi);

I had an error in the AndroidManifest.xml. 我在AndroidManifest.xml中出错了。 The NotificationPublisher needs to be registered as a receiver, like this: NotificationPublisher需要注册为接收者,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM