目前,对于处于后台模式的我的 Voip 应用程序,我使用下面的代码,当我的 voip 应用程序进入后台时或当它进入后台并在调用到达时锁定电话时,它会保持唤醒状态,它会显示通知。我看到在 IOS 8 和更高版本中 setKeepAliveTimeout 不再起作用,我们需要使用 Pushkit,是否可以在不对我们的服务器端进行编码的情况下使用 pushKit?
- (void)registerKeepAliveHandler:(UIApplication*) application
{
LogInfo(TAG_MAIN, @"Registering KeepAliveTimeout");
[application setKeepAliveTimeout:600 handler:^{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_KEEPALIVE_RECEIVED object:nil];
NSMutableString* statusStr = [[NSMutableString alloc] init];
switch ([[UIApplication sharedApplication] applicationState]) {
case UIApplicationStateActive:
[statusStr appendString:@"foreground"];
break;
case UIApplicationStateInactive:
[statusStr appendString:@"inactive"];
break;
case UIApplicationStateBackground:
[statusStr appendString:@"background"];
break;
default:
assert(0);
break;
}
LogInfo(TAG_MAIN, @"===================================== Keepalive received in %@ application status ===============================", statusStr);
[UIApplication getIPAddress];
LogInfo(TAG_MAIN, @"%@", [[UIDevice currentDevice] batteryInfo]);
[[SipEngine sharedInstance] isServerReachable];
[[SipEngine sharedInstance] isRegistered];
[[SipEngine sharedInstance] startStopRegistration];
[[[SipEngine sharedInstance] registration] registering];
}];
}
最佳答案
如果你想在终止状态下运行你的应用程序并了解来电并进行进一步处理,那么你应该使用推送套件静默通知。 (这样 Whatsapp 通话、Skype 等应用程序就可以工作了)
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
return YES;
}
#define PushKit Delegate Methods
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
if([credentials.token length] == 0) {
NSLog(@"voip token NULL");
return;
}
NSLog(@"PushCredentials: %@", credentials.token);
}
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
NSLog(@"didReceiveIncomingPushWithPayload");
}
@end
关于objective-c - 如何在Voip App中使用带有背景的pushkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302087/