2016-03-02 31 views
-1

我想弄清楚如何檢查基於區域的UILocalNotification是否被觸發。我搜索並找到了這個Remove fired Location-Based notification when user exits region,但它對我沒有幫助。需要幫助如何檢查基於區域的UILocalNotification是否被觸發

我已經配置了一個基於UILocalNotification的區域,當用戶輸入該特定區域時將會觸發該區域,並且當應用程序關閉時將會提供UILocalNotification

現在,當用戶打開應用程序時,我可以如何檢查基於UILocalNotification的區域是否被觸發。

回答

-1

使用gpx文件檢查基於區域的通知。

示例:創建一個文件Location.gpx,其中包含您希望觸發通知的位置的緯度。下面的代碼包含lat,long兩個位置。

<?xml version="1.0"?> 
<gpx version="1.1" creator="Xcode"> 
    <wpt lat="28.53191" lon="77.38311"> 
     <name>Google</name> 
    </wpt> 
    <wpt lat="28.50848" lon="77.40825"> 
     <name>Apple</name> 
    </wpt> 
</gpx> 

調試時,選擇Location.gpx文件,如下所示的圖像:

enter image description here

+0

我沒有問,我問是我的通知是否被解僱。 –

+0

如果您的通知被觸發,UIApplicationDelegate的[application:didReceiveLocalNotification](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification :)方法將被調用,並且您可以從userInfo通知字典中提取信息。 – PGDev

+0

但是我的應用在通知發佈時關閉。 –

0

代碼片段:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

    { 

     NSString *regionIdentifier = [notification.userInfo objectForKey:@"regionIdentifier」]; 

     [self removeExistingNotificationWithIdentifier: regionIdentifier]; 

    } 



    -(void)removeExistingNotificationWithIdentifier:(NSString*)regionIdentifier 
    { 
     NSArray *scheduledNotifications = [[UIApplication sharedApplication]scheduledLocalNotifications]; 
     for (UILocalNotification *notification in scheduledNotifications) 
     { 
      NSDictionary *userInfoDict = notification.userInfo; 
      if ([[userInfoDict objectForKey:@"regionIdentifier"] isEqualToString:regionIdentifier]) 
      { 
       [[UIApplication sharedApplication]cancelLocalNotification:notification]; 
      } 
     } 
    } 
+0

請提供您的答案的一些背景。 –

+0

上述方法用於接收本地通知,然後將它們從預定通知中刪除。請問清楚你想知道什麼。 – PGDev