首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不在ios10上调用

不在ios10上调用
EN

Stack Overflow用户
提问于 2016-09-19 12:56:49
回答 2查看 10.7K关注 0票数 6

我有两部iphones:一部有ios 10,2部有ios 9。

在尝试第一部iphone时:

当用户在警报上单击“允许”时,不会调用didRegisterForRemoteNotificationsWithDeviceToken方法,而是调用didRegisterUserNotificationSettings方法。在这种情况下,设备不接收推送通知。

在尝试第二部iphone时:

这两种方法都在这里被调用。设备确实接收推送通知。

然后我检查了模拟器ios 8

在这种情况下,与第一次相同。只有一个方法被调用。

我检查了一些类似的问题的答案,但他们没有帮助我。我怀疑这个问题是否存在于推送通知设置中,因为ios 9可以正常工作。所以问题就在ios 10的某个地方。

的主要问题是:

  1. 如何调用方法didRegisterForRemoteNotificationsWithDeviceToken
  2. 或者我如何获得设备令牌,因为它是目标

期待你的帮助!

EN

回答 2

Stack Overflow用户

发布于 2016-09-20 13:35:52

对于iOS 10使用xCode 8通用汽车。

通过以下步骤解决了这个问题。要求:- Xcode 8转基因种子。MAC OS :- EL 10.11.6船长

不要删除IOS 9或更低版本的代码。

步骤1:-转到-> Xcode中的目标设置->功能->启用PushNotifications。

步骤2:-添加UserNotifications框架->构建阶段->链接库

步骤3:-

代码语言:javascript
运行
复制
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

步骤4:-在方法didFinishLaunchingWithOptions寄存器中用于UIUserNotificationSettings。

代码语言:javascript
运行
复制
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    if(SYSTEM_VERSION_EQUALTO(@"10.0")){
        UNUserNotificationCenter *notifiCenter = [UNUserNotificationCenter currentNotificationCenter];
        notifiCenter.delegate = self;
        [notifiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
             if( !error ){
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
             }
         }];  
    }

    return YES;
    }

步骤5:-实现UNUserNotificationCenterDelegate的2个委托方法。

代码语言:javascript
运行
复制
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

//Do Your Code.................Enjoy!!!!
    }

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

}
票数 10
EN

Stack Overflow用户

发布于 2017-03-30 17:45:46

您应该在didFinishLaunchingWithOptions中调用此方法

代码语言: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.")
                    if !(SharedPrefs.sharedInstance!.isLoggedIn){
                        Defaults.sharedPref.removeNotificationToken()
                    }else{
                        UIApplication.shared.unregisterForRemoteNotifications()
                        print("We will delete the token at the time of logout")
                    }
                }
            }

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

https://stackoverflow.com/questions/39573792

复制
相关文章

相似问题

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