我有一个应用程序,可让两个人通过蓝牙玩游戏。当一个玩家发出邀请时,他们将手机变成灯塔。当这个信标打开时,其他人(如果在范围内)注册他们已经跨越到范围内,并且弹出一个通知。这在应用程序打开时很有用。但是,当应用程序处于后台时,会收到通知,但不会向用户显示,直到他们将应用程序唤醒为止。我该如何做到这一点,以便在应用程序处于后台模式时显示通知或播放声音或两者都播放。在后台获取信标通知iOS Xcode
这里是我用来接收region.state和显示通知的代码。
-(void)locationManager:(CLLocationManager*)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion*)region{
NSLog(@"manager didDetermineState state forRegion");
UILocalNotification *notification = [[UILocalNotification alloc]init];
if(state == CLRegionStateInside){
NSLog(@"region.identifier;%@",region.identifier);
if ([region.identifier isEqualToString:@"com.checkers.bluetooth"]) {
NSLog(@"region.identifier;%@",region.identifier);
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([UIScreen mainScreen].bounds.size.height == 480.0){
inviteCheckers.frame = CGRectMake(147,95 *.84,20,20);
}
else {
inviteCheckers.frame = CGRectMake(147,95,20,20);}
}
else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
inviteCheckers.frame = CGRectMake(64+294,145,40,40);
}
} completion:^ (BOOL completed) {
如果([YOURNAME isEqualToString:@ “无”]){ notification.alertBody = @ “你错过了有人邀请你玩跳棋\ n打开I'M GAME并发出邀请”; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } else { NSString * nameString1 = yourName; NSString * nameString2 = [nameString1 stringByAppendingString:@“想玩跳棋。”]; notification.alertBody = nameString2; AlertView = [[UIAlertView alloc] initWithTitle:@“玩游戏?” 消息:notification.alertBody
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"CANCEL", nil];
[self.AlertView setBackgroundColor:[UIColor clearColor]];
[AlertView show];} } ];
identifierString = region.identifier;
messageString2 = @"checkers";
recieve.hidden = NO;
}
//这是新的代码我已经加入
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if ([backgroundstring isEqualToString:@"fire"]) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Someone wants to play a game.";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
}
看来,这曾经工作,但现在没有。 我在位置更新后台模式下plist。 我得到的权限 现在,当应用程序在后台有一个邀请,但没有名字被记录通知出现,但在前景得到与该人的名字警报。
我已经添加了背景字符串从@“没有火”变化;开火”;当警报通知进入并且工作时。然后我试图在背景字符串是@“fire”时发送本地通知; (见上面的代码),但没有任何反应。我确定我错过了一些东西,但不知道是什么。 – user1114881
感谢提示。我会发布我的代码来反映我所做的。 – user1114881