I am trying to get the push notification token for my device in the mobile app for testing, but it is not being returned. How do you handle this?
What I do is to modify the AppDelegate of the generated application. Do you have any idea?
didRegisterForRemoteNotificationsWithDeviceToken is not executed, the code is as follows:#import "AppDelegate.h"
#import "MainViewController.h"
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)applicationUIApplication*)application didFinishLaunchingWithOptionsNSDictionary*)launchOptions
{
self.viewController = [[MainViewController alloc] init];
[self enableNotifications];
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void) enableNotifications{
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
}
- (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken{
// -- get token here....
}
- (void)applicationUIApplication *)application didFailToRegisterForRemoteNotificationsWithErrorNSError *)error{
NSLog(@"Error");
}
@end