Ios 注册远程通知没有任何作用
我的应用程序中有一个按钮,可以触发注册通知。假设这是一个Ios 注册远程通知没有任何作用,ios,objective-c,push-notification,Ios,Objective C,Push Notification,我的应用程序中有一个按钮,可以触发注册通知。假设这是一个UIBarButton UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Grant" style:UIBarButtonItemStyleDone target:self action:@selector(grantPermission:)]; self.navigationItem.rightBarButtonItem = rightButto
UIBarButton
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Grant" style:UIBarButtonItemStyleDone target:self action:@selector(grantPermission:)];
self.navigationItem.rightBarButtonItem = rightButton;
这是按钮触发的功能:
- (void)grantPermission:(id)sender {
if (SYSTEM_VERSION_LESS_THAN( @"10.0" )) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"Completion handler called");
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
} else {
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
}
}];
}
}
我还添加了以下委托方法:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Did Fail to Register for Remote Notifications");
NSLog(@"%@, %@", error, error.localizedDescription);
}
当我运行这段代码时,我得到请求许可的模态。当我按下'accept'时,我看到名为log的
完成处理程序,但之后什么都没有。不发生任何事情,不显示任何日志。没有什么。你知道我做错了什么吗?你必须在物理设备上测试代码。模拟器不获取通知令牌。您将得到一个错误回调
还要检查您针对的是哪个版本的iOS,因为应用程序中的回调在ios9-10(可能是11)之间发生了更改,因此您将有一个不推荐使用的方法,该方法不会被调用(或者visa也会被调用,具体取决于您测试的是哪个版本的iOS)您必须在物理设备上测试该代码。模拟器不获取通知令牌。您将得到一个错误回调
还要检查您针对的是哪个版本的iOS,因为应用程序中的回调在ios9-10(可能是11)之间发生了更改,因此您将有一个不推荐使用的方法,该方法不会被调用(或者visa国际组织也会被调用,具体取决于您测试的iOS版本)可能重复检查其所有答案,另外,您使用的是设备还是模拟器?因为你将无法通过模拟器获得令牌!所有答案都可能重复,另外,您是否使用设备或模拟器?因为你将无法通过模拟器获得令牌!