2015-11-24 43 views
3

我正在從qt 5.5中執行以下操作。項目。無法通過Android上的AlarmManager安排通知(使用Qt)

我想在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(所以通知應該立即出現)。

Lod.d("!","Scheduled")輸出出現在控制檯輸出中,但Log.d("!", "Notified")沒有。那麼我是否安排警報不正確?

回答

2

我在AndroidManifest.xml中有錯誤。 NotificationPublisher需要註冊爲接收者,如下所示:

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

老實說不是100%確定你爲什麼不工作,但我懷疑它是0被傳入。我會認爲這應該是System.currentTimeInMillis()+ SOME_SHORT_INTERVAL?

這是Alarm Manager設置的一個工作示例。是的,我知道它的不同,但它是比較的東西

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

嘿,感謝您的回覆。事實證明,這在Manifest中是一個錯誤 – Nathan