【问题标题】:iOS Push NotificationiOS 推送通知
【发布时间】:2017-04-19 15:43:30
【问题描述】:

我使用 APNS,它在 iOS 9 上运行良好。 随着 iOS10 上新的推送 API 更改,我无法注册推送通知,因此我插入了下一个更改:

  1. 在目标功能选项卡中启用推送通知。
  2. 在 didFinishLaunchingWithOptions 我们检查操作系统版本并注册如下:

    if (SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) {
    
        //ios 10 Notifiction
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
            if( !error ){
                [[UIApplication sharedApplication] registerForRemoteNotifications];
                NSLog(@"iOS 10 push notification register successfully ");
            }
        }];
    } else {
        // iOS 8-9 Notifications
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        NSLog(@"iOS 9 push notification register successfully ");
    
    }
    

通过这些更改,我设法在 iOS 9 和 iOS 10 中执行注册,但我遇到了几个问题:

  1. 在目标功能选项卡中启用推送通知后,尽管注册已成功完成,但推送通知在 iOS 9 上停止工作。
  2. 虽然注册成功,但推送在 iOS 10 上根本不起作用。

请记住,如果我在目标功能选项卡中关闭推送通知(使用相同的代码),推送将在 iOS 9 上恢复工作,但我无法在 iOS 10 上注册 APNS。

【问题讨论】:

  • 也已经尝试过了。在功能选项卡中启用推送通知,添加 UserNotifications 框架和 UNUserNotificationCenterDelegate,检查操作系统版本并在两个操作系统版本中成功注册。问题是 didReceiveRemoteNotification 没有调用。我不需要知道通知中的用户操作,但我也实现了 didReceiveRemoteNotification:userInfo fetchCompletionHandler

标签: ios objective-c push-notification


【解决方案1】:
    if #available(iOS 8.0, *) {
        let settings: UIUserNotificationSettings = UIUserNotificationSettings (types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }else {
        let types: UIRemoteNotificationType = [.alert, .badge, .sound]
        application.registerForRemoteNotifications(matching: types)
    }

【讨论】:

  • 谢谢,问题不在注册,是推送接收
猜你喜欢
  • 2011-08-07
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多