В iOS 10
мы должны позвонить UNUserNotificationCenter
в appDelegate didFinishLaunchingWithOptions
.
Вы должны импортировать UserNotifications.framework
и добавить UNUserNotificationCenterDelegate
в Appdelegate
AppDelegate.h
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[[UIDevice currentDevice]systemVersion]floatValue]<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)
{
if(!error)
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"Push registration success.");
}
else
{
NSLog(@"Push registration FAILED");
NSLog(@"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription);
NSLog(@"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion);
}
}];
}
return YES;
}
Для получения информации в Swift See this
Да, я реализовал код iOS 10 и отлично работает на iPad, но не на iPhone 6 с iOS 10. Я получаю предупреждение дважды, если возможно, просмотрите видео. –
@PareshVasoya вы можете проверить свой метод didFinishLaunchingWithOptions, который вы дважды не вызываете «registerUserNotificationSettings». –
Да, я дважды это проверил, но зарегистрировался только один раз в методе didFinishLaunchingWithOptions для iOS 10. –