Skip to content

Commit f83482f

Browse files
Notifications iOS 10
1 parent fd986ff commit f83482f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Push Notifications in iOS 10 [Objective-C]
2+
3+
<p align="justify">The new framework called "UserNotifications" is introduced with iOS 10 SDK. The <a href="https://developer.apple.com/reference/usernotifications" target="_blank">UserNotifications framework</a> (UserNotifications.framework) supports the delivery and handling of local and remote notifications.
4+
5+
So, Let see what we have to change to get the push notifications in iOS 10.
6+
</p>
7+
8+
##Steps for implement code to handle push notifications in iOS 10
9+
10+
###Import UserNotifications.framework in your AppDelegate file
11+
12+
```objective-c
13+
#import <UserNotifications/UserNotifications.h>
14+
```
15+
Also add UNUserNotificationCenterDelegate.
16+
17+
```objective-c
18+
#import <UserNotifications/UserNotifications.h>
19+
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
20+
21+
@end
22+
```
23+
###Register for push notification
24+
25+
<p align="justify">Before registration check the version of iOS and then based on versions do the code. For iOS 7 code was different, fro iOS 8 & 9 code was different and again for iOS 10 code is different.</p>
26+
27+
<p align="justify"><em>As per my opinion you have to set the deployment target to iOS 8 or iOS 9 and later. For this you can check the <a href="https://developer.apple.com/support/app-store/" target="_blank">adoption ratio of iOS</a> in the devices.</em></p>
28+
29+
<strong>Define constant for version check :</strong>
30+
31+
```objective-c
32+
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
33+
```
34+
35+
<strong>Add code in your did finish launching</strong>
36+
37+
```objective-c
38+
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
39+
[self registerForRemoteNotifications];
40+
return YES;
41+
}
42+
43+
- (void)registerForRemoteNotifications {
44+
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
45+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
46+
center.delegate = self;
47+
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
48+
if(!error){
49+
[[UIApplication sharedApplication] registerForRemoteNotifications];
50+
}
51+
}];
52+
}
53+
else {
54+
// Code for old versions
55+
}
56+
}
57+
```
58+
59+
###Handling delegate methods for UserNotifications
60+
61+
<p align="justify"><strong><em>You will be surprise that notification displayed when application in foreground too in iOS 10. As we know that in old versions we display alert or something else which will be look like notification comes in foreground.</em></strong></p>
62+
63+
There are two delegate methods need to be handled :
64+
65+
```objective-c
66+
67+
//Called when a notification is delivered to a foreground app.
68+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
69+
NSLog(@"User Info : %@",notification.request.content.userInfo);
70+
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
71+
}
72+
73+
//Called to let your app know which action was selected by the user for a given notification.
74+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
75+
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
76+
completionHandler();
77+
}
78+
```
79+
80+
###Add Push Notifications Entitlements
81+
82+
<p align="justify">Go to your project target's <strong>Capabilities</strong> tab and add Push Notifications Entitlements.</p>
83+
84+
<img src="http://ashishkakkad.com/wp-content/uploads/2016/09/Push-Notifications-Entitlements.png" alt="Push Notifications Entitlements" />
85+
86+
<p align="justify">If it's available in your certificates then it will enable directly else configure your profile with the certificates and you can enable this capability by that.</p>
87+
88+
<b>Blogpost on my website : <a href="http://ashishkakkad.com/2016/09/push-notifications-in-ios-10-objective-c/" target="_blank">Push Notifications in iOS 10 [Objective-C]</a><b>

0 commit comments

Comments
 (0)