在后台时获取信标通知 iOS Xcode
get beacon notifications when in background iOS Xcode
我有一个应用可以让两个人通过蓝牙玩游戏。当一名玩家发出游戏邀请时,他们正在将 phone 变成灯塔。当此信标打开时,其他使用该应用程序的人(如果在范围内)注册他们已经进入范围并弹出通知。这在应用程序打开时效果很好。但是,当应用程序在后台时,会收到通知但不会显示给用户,直到他们唤醒应用程序。当应用程序处于后台模式时,如何才能显示通知或播放声音或两者兼而有之。
这是我用来接收 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) {
if ([yourName isEqualToString:@"None"]) {
notification.alertBody = @"You missed someone inviting you to play Checkers.\nOpen I'M GAME and send out an invite.";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
别的 {
NSString *nameString1 = 你的名字;
NSString *nameString2 = [nameString1 stringByAppendingString:@"想下跳棋。"];
notification.alertBody = nameString2;
AlertView= [[UIAlertView alloc] initWithTitle:@"Play a Game?"
message: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 的后台模式下有位置更新。
我获得权限
现在,当应用程序在后台时,有一个邀请但没有记录姓名,会出现一个通知,但在前台会收到有关此人姓名的警报。
代码引用了 UILocalNotification
但没有发送。相反,它会显示一个 UIView
和一个警告对话框,这些操作仅在应用程序位于前台时可见。
为了在应用程序不在前台时提醒用户,iOS 为您提供的唯一工具是 UILocalNotification
。如果应用程序在后台,您必须将其呈现给用户。如果用户点击它,它会将应用带到前台。
我有一个应用可以让两个人通过蓝牙玩游戏。当一名玩家发出游戏邀请时,他们正在将 phone 变成灯塔。当此信标打开时,其他使用该应用程序的人(如果在范围内)注册他们已经进入范围并弹出通知。这在应用程序打开时效果很好。但是,当应用程序在后台时,会收到通知但不会显示给用户,直到他们唤醒应用程序。当应用程序处于后台模式时,如何才能显示通知或播放声音或两者兼而有之。
这是我用来接收 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) {
if ([yourName isEqualToString:@"None"]) { notification.alertBody = @"You missed someone inviting you to play Checkers.\nOpen I'M GAME and send out an invite."; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } 别的 { NSString *nameString1 = 你的名字; NSString *nameString2 = [nameString1 stringByAppendingString:@"想下跳棋。"]; notification.alertBody = nameString2; AlertView= [[UIAlertView alloc] initWithTitle:@"Play a Game?" message: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 的后台模式下有位置更新。 我获得权限 现在,当应用程序在后台时,有一个邀请但没有记录姓名,会出现一个通知,但在前台会收到有关此人姓名的警报。
代码引用了 UILocalNotification
但没有发送。相反,它会显示一个 UIView
和一个警告对话框,这些操作仅在应用程序位于前台时可见。
为了在应用程序不在前台时提醒用户,iOS 为您提供的唯一工具是 UILocalNotification
。如果应用程序在后台,您必须将其呈现给用户。如果用户点击它,它会将应用带到前台。