1
我的應用程序中有一個按鈕,用於觸發註冊通知。說這是一個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);
}
當我運行這段代碼,我得到的模態請求許可。當我按'接受'時,我看到Completion handler called
日誌,但沒有任何記錄。沒有任何反應,沒有顯示日誌。沒有。任何想法,我做錯了什麼?
[爲什麼didRegisterForRemoteNotificationsWithDeviceToken不叫(https://stackoverflow.com/questions/4086599/why-didregisterforremotenotificationswithdevicetoken-is-not-called) – Honey
圍棋的可能的複製,另外你使用的是設備還是模擬器?因爲你將無法通過模擬器獲得令牌! – Honey