首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xcode8中不调用

在Xcode8中不调用
EN

Stack Overflow用户
提问于 2016-09-09 12:11:37
回答 2查看 7.4K关注 0票数 3

我使用iOS 10SDK在Xcode 8上构建我的应用程序。iOS 9.3设备未调用didRegisterForRemoteNotificationsWithDeviceToken,因此iOS 9.3及以下设备无法接收推送通知。但iOS 10设备可以接收推送。如果我使用iOS 9.3SDK在Xcode7.3上构建相同的应用程序,那么iOS 9设备就可以接收推送。所以,我确信我的所有设置都应该是正确的。

这是我的实现:

代码语言:javascript
复制
// 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);
}
EN

回答 2

Stack Overflow用户

发布于 2016-09-09 13:03:34

为什么要使用过时的方法??[[UIApplication sharedApplication] registerForRemoteNotificationTypes:

当我移除它的时候

代码语言:javascript
复制
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)categories:nil]];

并使用

代码语言:javascript
复制
[[UIApplication sharedApplication] registerForRemoteNotifications];

我从APN拿到了令牌。

票数 2
EN

Stack Overflow用户

发布于 2017-03-31 01:52:18

您需要从didFinishLaunchingWithOptions调用registerForNotifications方法。

代码语言:javascript
复制
 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))
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39403752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档