我有两部iphones:一部有ios 10,2部有ios 9。
在尝试第一部iphone时:
当用户在警报上单击“允许”时,不会调用didRegisterForRemoteNotificationsWithDeviceToken
方法,而是调用didRegisterUserNotificationSettings
方法。在这种情况下,设备不接收推送通知。
在尝试第二部iphone时:
这两种方法都在这里被调用。设备确实接收推送通知。
然后我检查了模拟器ios 8
在这种情况下,与第一次相同。只有一个方法被调用。
我检查了一些类似的问题的答案,但他们没有帮助我。我怀疑这个问题是否存在于推送通知设置中,因为ios 9可以正常工作。所以问题就在ios 10的某个地方。
的主要问题是:
didRegisterForRemoteNotificationsWithDeviceToken
期待你的帮助!
发布于 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:-
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
步骤4:-在方法didFinishLaunchingWithOptions寄存器中用于UIUserNotificationSettings。
-(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个委托方法。
-(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{
}
发布于 2017-03-30 17:45:46
您应该在didFinishLaunchingWithOptions中调用此方法
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()
}
}
https://stackoverflow.com/questions/39573792
复制相似问题