推送通知问题与iOS 10

我已经开发了一个应用程序,我已经实现了推送通知。 目前它在苹果商店生活。 Upto的iOS 9推进工作正常,但在iOS 10后,它不工作。

代码有什么问题?

对于使用xCode 8 GM的iOS 10。

我已经解决了我的问题,使用xCode 8 GM的iOS 10下面的步骤:

1)在目标中,在function下启用推送通知以添加推送通知权利。

2)实现UserNotifications.framework到您的应用程序。 在您的AppDelegate中导入UserNotifications.framework。

#import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> @end 

3)在didFinishLaunchingWithOptions方法中分配UIUserNotificationSettings并实现UNUserNotificationCenter委托。

 #define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if( !error ){ [[UIApplication sharedApplication] registerForRemoteNotifications]; } }]; } return YES; } 

4)现在终于实现这两个委托方法。

// ============对于iOS 10 =============

 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ //Called when a notification is delivered to a foreground app. NSLog(@"Userinfo %@",notification.request.content.userInfo); completionHandler(UNNotificationPresentationOptionAlert); } -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ //Called to let your app know which action was selected by the user for a given notification. NSLog(@"Userinfo %@",response.notification.request.content.userInfo); } 

请保持代码,因为它是你使用的iOS 9,只需添加代码行来支持使用UserNotifications.framework的iOS 10推送通知。

在iOS 10之前一切工作正常,在我的情况下只有能力设置导致这个问题。

必须打开推送通知。

在这里输入图像描述

我遇到了iOS 10静音推送通知的问题。 在iOS9和更早版本中,发送具有附加数据字段但在数据中具有空的aps属性的推送通知正常工作。 但是在iOS10中,带有空aps属性的推送通知根本不会触及didReceiveRemoteNotification应用程序的委托方法,这意味着我所有的静默推送通知(我们只是在内部用来触发应用程序打开时的操作的通知)在iOS10中停止工作。

我能够解决这个问题,而不是推送更新到我的应用程序,通过至less添加一个属性到推送通知的aps部分,在我的情况下,我只是添加了badge: 0和我的静默推送通知在iOS 10中再次开始工作。希望这可以帮助别人!

首先,前往您的应用程序目标。 selectfunction,并确保启用“推送通知”,并在后台模式下select“远程通知”。

在这里输入图像描述

在这里输入图像描述

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { var token = "" for i in 0..<deviceToken.count { token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) } print(token) 

}

@Ashish Shah代码的swift 3版本是:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //notifications if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.delegate = self center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in if error == nil{ UIApplication.shared.registerForRemoteNotifications() } } } else { // Fallback on earlier versions } return true } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { } 

不要忘了,testing时,您必须使用sandbox地址来使通知正常工作。

在iOS上,通过调用UIApplicationregisterUserNotificationSettings:策略,应用程序接近客户端进行授权以获得推送警告。

应用程序调用UIApplication (iOS)的registerForRemoteNotifications:技术或NSApplication (OS X)的策略registerForRemoteNotificationTypes: NSApplication

应用application:didRegisterForRemoteNotificationsWithDeviceToken:UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)执行application:didRegisterForRemoteNotificationsWithDeviceToken:技术,以获取由推送收益生成的一种小工具标记。

应用application:didFailToRegisterForRemoteNotificationsWithError:UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)执行application:didFailToRegisterForRemoteNotificationsWithError:技术,如果注册失败,则会出现错误。