我想在我的iOS应用程序中实现VoIP通知,但 didUpdatePushCredentials 方法从未被调用,我无法获取设备令牌。
我在应用程序中实现了APNS,这两个服务可能冲突吗?
这是我的AppDelegate代码
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
LOGI(@"%@", NSStringFromSelector(_cmd));
//register for voip notifications
self->pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
[self->pushRegistry setDelegate:self];
[self->pushRegistry setDesiredPushTypes:[NSSet setWithObject:PKPushTypeVoIP]];
NSLog(@"VoIP push registered");
}
#pragma mark - VoIP push methods
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
NSLog(@"voip token: %@", credentials.token);
}
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSDictionary *payloadDict = [payload.dictionaryPayload valueForKey:@"aps"];
NSString *message = (NSString *)[payloadDict valueForKey:@"alert"];
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [message stringByAppendingString:@" - voip"];
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = @"notes_of_the_optimistic.caf";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VoIP notification" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
});
}
NSLog(@"incoming voip notfication: %@", payload.dictionaryPayload);
}
- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type {
NSLog(@"Voip token invalidate");
}
任何让它运作的解决方案?
答案 0 :(得分:15)
如果您正在运行较新的xcode(我在xcode 9上),则VOIP不在Capabilities选项卡的Background部分中。这样可以防止调用didUpdatePushCredentials
!
诀窍是你必须进入你的plist,并且在Required Background Modes
中你需要添加App provides Voice over IP services
。
我无法相信Apple已经对我们这样做了。我浪费了8个小时的工作日来寻找这个简单的解决方案。
答案 1 :(得分:2)
使用以下代码并确保遵循以下事项。
**`**// Register for VoIP notifications**`**
- (void) voipRegistration {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// Create a push registry object
_voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue];
// Set the registry's delegate to self
[_voipRegistry setDelegate:(id<PKPushRegistryDelegate> _Nullable)self];
// Set the push type to VoIP
_voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}
在didFinishLaunchingWithOptions
其他删除方法如下
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials: (PKPushCredentials *)credentials forType:(NSString *)type {
// Register VoIP push token (a property of PKPushCredentials) with server
if([credentials.token length] == 0) {
NSLog(@"voip token NULL");
return;
}
NSLog(@"%@",credentials.token);
}
//在项目能力中确保这一点
1)背景模式开启
2)VOIP
3)背景提取
4)remotenotification
5)别忘了导入代表
在背景模式
中答案 2 :(得分:1)
证书存在问题。
重新生成并重新导入证书并解决问题。
答案 3 :(得分:0)
对我来说,解决方案是启用:
P.S。我没有忘记启用 但是,启用一般推动也很重要。完整的代码是:
import UIKit
import PushKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
voipRegistration()
return true
}
// Register for VoIP notifications
func voipRegistration() {
let mainQueue = DispatchQueue.main
// Create a push registry object
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [.voIP]
}
}
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
print("voip token = \(token)")
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
print("payload = \(payload.dictionaryPayload)")
}
}