简体   繁体   中英

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

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

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

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

    }
}

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

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?

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 ?

This is a working example of an Alarm Manager setup. 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. The NotificationPublisher needs to be registered as a receiver, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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