2014-01-10 27 views
1

我想獲得本地通知,隨時打開我的手機,並且我處於特定區域內。這按預期工作,但每次我打開設備時,都會收到新通知。如果我只是離開現有的通知,它可以被推到通知列表的底部。如果我取消現有通知並創建一個新通知,我會看到一個奇怪的動畫。有沒有辦法:CLBeaconRegion notifyEntryStateOnDisplay和UILocalNotifications

  1. 更新已經交付的現有UILocalNotification推到頂部。
  2. 當鎖定屏幕消失並取消通知時會以某種方式得到通知?

這裏是我現有的代碼:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { 
    if ([region isKindOfClass:[CLBeaconRegion class]]) { 
     CLBeaconRegion *beaconRegion = (CLBeaconRegion*)region; 
     UILocalNotification *existingNotification = self.beaconNotification; 
     switch (state) { 
      case CLRegionStateInside: 
       self.beaconNotification = [[UILocalNotification alloc] init]; 
       self.beaconNotification.alertBody = @"You're inside the region"; 
       [[UIApplication sharedApplication] presentLocalNotificationNow:self.beaconNotification]; 
       if (existingNotification) { 
        [[UIApplication sharedApplication] cancelLocalNotification:existingNotification]; 
       } 
       break; 
      case CLRegionStateOutside: 
       self.beaconNotification = [[UILocalNotification alloc] init]; 
       self.beaconNotification.alertBody = @"You're outside the region"; 
       [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
       break; 
      default: 
       break; 
     } 
    } 
} 

回答

0

有一定的方式,如果手機被鎖定檢測/解鎖Is there a way to check if the iOS device is locked/unlocked?

對於進一步的通知,我建議你看看這個名單:http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications 它包含在手機關機時調用的com.apple.springboard.deviceWillShutDown通知。我只是用這段代碼測試了它,並且似乎可行,但是,該應用程序立即死亡,並且XCode會話終止,因此我不確定這對於真實應用程序有多麼有用。

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
            NULL, 
            shutdownCallback, 
            CFSTR("com.apple.springboard.deviceWillShutDown"), 
            NULL, 
            CFNotificationSuspensionBehaviorDeliverImmediately); 
void shutdownCallback (CFNotificationCenterRef center, void *observer, 
       CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{ 
    NSLog(@"Shutting down"); 
} 

考慮到奇怪的動畫,如果你不喜歡它,然後向Apple報告。只有他們可以解決它。我認爲你不應該更新通知(如果這是可能的話)。只需推一個新的,並刪除舊的或不打擾它。這是大多數應用程序所做的,用戶通常不會遇到任何問題。它也是一種歷史。

+0

是的,我可以檢測到手機是鎖定還是解鎖,但當他們打開手機時我會收到通知,並且在他們關閉手機時我不會再收到通知。 –

+0

我不確定是否完全瞭解您的通知問題,但我在答案中添加了更多詳細信息。讓我知道它是否有幫助。 – allprog

相關問題