DidUpdatePushCredentials not called
I want to implement VoIP notifications in my iOS app, but the didUpdatePushCredentials method was never called, I cannot get the device token.
I have applied APNS in my application, can these two services conflict?
Here are my AppDelegate codes
- (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");
}
- I have enabled remote notifications, certification profiles and profiles installed.
- I can send standard notifications using APNS.
Any solution to get it working?
source to share
If you are using newer xcode (I'm on xcode 9) then VOIP is not in the Background section of the Features tab. This will prevent the challenge didUpdatePushCredentials
!
The trick is that you need to go to your layer, and in Required Background Modes
you need to add App provides Voice over IP services
.
I can't believe Apple did this to us. I spent an 8 hour working day looking for this simple fix.
source to share
Use the code below and verify the following.
**`**// 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 delegate to self
[_voipRegistry setDelegate:(id<PKPushRegistryDelegate> _Nullable)self];
// Set the push type to VoIP
_voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}
Method call below in didFinishLaunchingWithOptions
Another removal method as follows
- (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);
}
// Make sure about this in the project capabilities
1) Background mode ON
2) VOIP
3) Background fetch
4) remotenotification
5) Didn't forget to import the delegate
inside background mode
source to share