Push Notification with iOS 10

I developed one application that implements push notification. He currently lives in an Apple store. Upto iOS 9 push works fine, but after iOS 10 it doesn't work.

What is the problem with the code?

+49
ios ios10 push-notification
Sep 14 '16 at 12:34
source share
7 answers

For iOS 10 using xCode 8 GM.

I solved the problem with the following steps using xCode 8 GM for iOS 10:

1) For targeted purposes, in the "Features" section, click "Push Notifications" to add Push Push notifications.

2) Embed UserNotifications.framework in your application. Import UserNotifications.framework into AppDelegate.

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

3) In the didFinishLaunchingWithOptions method, assign UIUserNotificationSettings and run the UNUserNotificationCenter delegate.

 #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) Now, finally, we implement two delegate methods.

// ============= For 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); } 

Please stay in the code since it is used for iOS 9. Only add lines of code to support push notifications for iOS 10 with UserNotifications.framework.

+112
Sep 15 '16 at 8:36
source share

Everything worked perfectly up to iOS 10. In my case, only configuration problems cause this problem.

It must be enabled for push notifications.

enter image description here

+24
Sep 21 '16 at 9:28
source share

I had a problem with tick push notifications of iOS 10. In iOS9 and earlier, sending a push notification with additional data fields, but had an empty aps attribute in the data that worked fine. But in iOS10, a push notification with an empty aps attribute does not delete the didReceiveRemoteNotification application delegation method at all, which means that all my silent push notifications (notifications that we just use to trigger actions when the application opens) have stopped working iOS10.

I managed to fix this without pushing the update for my application, adding at least one attribute to the aps part of the push notification, in my case I just added badge: 0 , and my silent push notifications started working again in iOS 10. I hope that it will help someone else!

+18
Oct 05 '16 at 12:57
source share

First go to the destination of the application. Select "Features" and make sure "Push Notifications" is turned on and that "Remote notifications are selected in the" Background modes ".

enter image description here

enter image description here

 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) 

}

+12
Sep 26 '16 at 7:03
source share

Fast version 3 of the code @Ashish Shah:

 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) { } 
+7
Sep 22 '16 at 2:37
source share

Do not forget that during testing you should use the sandbox address for your notifications to work.

0
Dec 28 '16 at 7:34
source share

In iOS, the application suits the client for authorization to receive push alerts by invoking the registerUserNotificationSettings: strategy for UIApplication .

An application calls the registerForRemoteNotifications: method for UIApplication (iOS) or the registerForRemoteNotificationTypes: NSApplication (OS X) strategy.

The application executes the application:didRegisterForRemoteNotificationsWithDeviceToken: method for UIApplicationDelegate (iOS) or NSApplicationDelegate (OS X) to get one of the gadget tokens created by the push benefit.

The application executes the application:didFailToRegisterForRemoteNotificationsWithError: method for UIApplicationDelegate (iOS) or NSApplicationDelegate (OS X) to get an error if registration starts.

-one
Oct 28 '16 at 12:16
source share



All Articles