在 Xcode 8 中未调用 didRegisterForRemoteNotificationsWithDeviceToken
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39403752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didRegisterForRemoteNotificationsWithDeviceToken is not called in Xcode 8
提问by smartsanja
I build my app on Xcode 8
with iOS 10 SDK.
didRegisterForRemoteNotificationsWithDeviceToken
is not called for iOS 9.3 device, hence iOS 9.3 and bellow devices unable to receive push notifications.
But iOS 10 devices can receive push.
If i build the same app on Xcode 7.3 with iOS 9.3 SDK, then iOS 9 devices can receive push.
So, Im sure that all my settings should be correct.
我Xcode 8
使用 iOS 10 SDK构建我的应用程序。
didRegisterForRemoteNotificationsWithDeviceToken
不为 iOS 9.3 设备调用,因此 iOS 9.3 和以下设备无法接收推送通知。但是 iOS 10 设备可以接收推送。如果我使用 iOS 9.3 SDK 在 Xcode 7.3 上构建相同的应用程序,那么 iOS 9 设备可以接收推送。所以,我确信我的所有设置都应该是正确的。
This is my implementation :
这是我的实现:
// Push notification
if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
{
if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString * tokenAsString = [[[deviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" " withString:@""];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
DLog(@"app didFailToRegisterForRemoteNotificationsWithError: %@", error.description);
}
回答by Ananth
Why are you using deprecated method ?? [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
你为什么使用不推荐使用的方法? [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
When I removed it
当我删除它时
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)categories:nil]];
and used
并使用
[[UIApplication sharedApplication] registerForRemoteNotifications];
I got token from APNs.
我从 APNs 得到了令牌。
回答by Ved Rauniyar
You need to call registerForNotifications method from didFinishLaunchingWithOptions.
您需要从 didFinishLaunchingWithOptions 调用 registerForNotifications 方法。
func registerForNotifications(){
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options:[.alert,.sound,.badge]) { (granted, error) in
if granted{
UIApplication.shared.registerForRemoteNotifications()
}else{
print("Notification permission denied.")
}
}
} else {
// For iOS 9 and Below
let type: UIUserNotificationType = [.alert,.sound,.badge];
let setting = UIUserNotificationSettings(types: type, categories: nil);
UIApplication.shared.registerUserNotificationSettings(setting);
UIApplication.shared.registerForRemoteNotifications()
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = String(format: "%@", deviceToken as CVarArg).trimmingCharacters(in: CharacterSet(charactersIn: "<>")).replacingOccurrences(of: " ", with: "")
print(token)
}
extension AppDelegate : UNUserNotificationCenterDelegate{
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground”)
let info = ((notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary)
if let type = info.value(forKey: "type") as? Int{
if type == 0 { // notification arrive
handlePush(apsDict: (notification.request.content.userInfo as NSDictionary))
}
}
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Handle push from background or closed")
let info = ((response.notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary)
if let type = info.value(forKey: "type") as? Int{
if type == 0 { // notification arrive
handlePush(apsDict: (response.notification.request.content.userInfo as NSDictionary))
}
}
}
}