如何设置iBeacon仅针对单个设备广播本地通知一次?

时间:2014-05-15 07:50:28

标签: ios objective-c broadcast advertising ibeacon

每当我们进入或退出

区域时,我都会提出本地通知
locationManager:didRangeBeacons:inRegion method of CLLocationManagerDelegate:
像这样......

//local notification sent from transmitter to receiver 

switch (beacon.proximity) 
{  
 case CLProximityNear:   
   message = @"You are near the ibeacon device";  
   bgColor = [UIColor blueColor];  
   break;  
 case CLProximityImmediate:   
   message = @"You are very closer to the device..";  
   bgColor = [UIColor colorWithRed:.0f green:.0f blue:230.0f alpha:1.0f];  
   break; 
}

现在,当接收器检测到发射器并发送本地通知时,接收器将获得本地通知。另外,我想设置一天的本地通知,即接收方应该只从发射机接收一次消息。非常感谢宝贵的建议。

注意:我正在使用iPad mini作为发射器和放大器。 iPhone 5作为接收器。

3 个答案:

答案 0 :(得分:2)

您需要跟踪didRangeBeaconsInRegion方法中检测到每个信标的时间,如果最近看到它们,请使用软件过滤器忽略某些信标。

显示了如何执行此操作的示例here.其核心是:

  - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLRegion *)region
  {
      for (CLBeacon *beacon in beacons) {
          Boolean shouldSendNotification = NO;
          NSDate *now = [NSDate date];
          NSString *beaconKey = [NSString stringWithFormat:@"%@_%ld_%ld", [beacon.proximityUUID UUIDString], (long) beacon.major, (long) beacon.minor];
          NSLog(@"Ranged UUID: %@ Major:%ld Minor:%ld RSSI:%ld", [beacon.proximityUUID UUIDString], (long)beacon.major, (long)beacon.minor, (long)beacon.rssi);

          if ([beaconLastSeen objectForKey:beaconKey] == Nil) {
              NSLog(@"This beacon has never been seen before");
              shouldSendNotification = YES;
          }
          else {
              NSDate *lastSeen = [beaconLastSeen objectForKey:beaconKey];
              NSTimeInterval secondsSinceLastSeen = [now timeIntervalSinceDate:lastSeen];
              NSLog(@"This beacon was last seen at %@, which was %.0f seconds ago", lastSeen, secondsSinceLastSeen);
              if (secondsSinceLastSeen < 3600*24 /* one day in seconds */) {
                  shouldSendNotification = YES;
              }
          }

          if (shouldSendNotification) {
              [self sendLocalNotification];
          }
      }
  }

答案 1 :(得分:1)

是的,有可能。请尝试以下代码: -

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
BOOL alreadySent = [userDefault boolForKey:[NSString stringWithFormat:@"%@",region.identifier]];

if(!alreadySent){

    //Already Send the notification for today
    [userDefault setBool:YES forKey:[NSString stringWithFormat:@"%@",region.identifier]];

    //Set the date for this region, you will need this date in order to set the BOOL to No later.
    [userDefault setObject:[NSDate date] forKey:[NSString stringWithFormat:@"%@Date",region.identifier]];

    //TODO: Send the Local Notification here
}

//TODO: Somewhere else, you will have to check if the date for that particular region.identifier
//if the date is already over 24 hours, set the Bool to NO

答案 2 :(得分:1)

当您检测到信标并通知用户时,您可以将当前日期存储在持久性存储中,例如NSUserDefaults。然后,当您下次检测到该信标/区域时,您可以检查日期,如果它是&#34;今天&#34;,则什么都不做。如果它不是今天&#34;然后再次通知用户并更新存储的值