How to use push notification in ios 10 with swift
In ios10 apple introduce UserNotifications framework, Apple has given us the ability to make notifications that contain images, sound, video, or even custom content generated on the device. Much of this capability comes via three new features showcased at WWDC: media attachments, notification service extensions, and notification content extensions.
Framework
UserNotifications
Overview
The UserNotifications framework (UserNotifications.framework) supports the delivery and handling of local and remote notifications. You use the classes of this framework to schedule the delivery of local notifications based on specific conditions, such as time or location. Apps and extensions also use this framework to receive and potentially modify local and remote notifications when they are delivered to the user’s device.
Classes
UNCalendarNotificationTrigger
Triggers a notification at the specified date and time.
UNLocationNotificationTrigger
Triggers the delivery of a notification when the user reaches the specified geographic location.
UNMutableNotificationContent
Provides the editable content for a notification.
UNNotification
Contains the data for a delivered notification.
UNNotificationAction
Defines a task to perform in response to a delivered notification.
UNNotificationAttachment
Manages media content associated with a notification.
UNNotificationCategory
Defines the types of notifications your app supports and the custom actions displayed for each type.
UNNotificationContent
Stores the content of a local or remote notification.
UNNotificationRequest
Encompasses a notification’s content and the condition that triggers its delivery.
UNNotificationResponse
Contains the user’s response to an actionable notification.
UNNotificationServiceExtension
Modifies the content of remote notifications before they are delivered to the user.
UNNotificationSettings
Contains the notification-related settings and authorization status of your app
UNNotificationSound
Represents a sound to be played when a notification is delivered.
UNNotificationTrigger
Provides common behavior for subclasses that trigger the delivery of a notification.
UNPushNotificationTrigger
Indicates that a delivered notification was sent using the Apple Push Notification Service.
UNTextInputNotificationAction
Defines an action that contains user-specified text.
UNTextInputNotificationResponse
Contains the user’s response to an actionable notification, including any custom text that the user typed or dictated.
UNTimeIntervalNotificationTrigger
Triggers the delivery of a local notification after the specified amount of time.
UNUserNotificationCenter
Manages the notification-related activities for your app or app extension.
Protocols
UNUserNotificationCenterDelegate
Handles notification-related interactions for your app or app extension.
Lets Start
Step 1: Create certificataes with using your developer account and don’t forgot to enable push notification services.
Step 2: Create new project
Step 3: Click to Targets then click to general and select team or select valid provisioning profile, make sure your bundle identifies is same with certificate bundle id.
Step 4: Click to capabilities and enable push notification.
Step 5: Open AppDelegate.swift
Step 6: import UserNotifications framework
Step 7: Add UNUserNotificationCenterDelegate in appDelegate class
Step 8: Now register for push notification
// Code
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {//register for push notification registerForPushNotifications(application)}func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. //showFirstController() } func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } func registerForPushNotifications(application: UIApplication) { UNUserNotificationCenter.currentNotificationCenter().delegate = self UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in if (granted) { UIApplication.sharedApplication().registerForRemoteNotifications() } else{ //Do stuff if unsuccessful... } }) } // Push notification delegate func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { print(error) } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("DEVICE TOKEN = (deviceToken)") //let token = JSON(device_token) device_token = "(deviceToken)" device_token = device_token.stringByReplacingOccurrencesOfString("<", withString: "") device_token = device_token.stringByReplacingOccurrencesOfString(">", withString: "") device_token = device_token.stringByReplacingOccurrencesOfString(" ", withString: "") print(device_token) } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { print(userInfo) }
Once you are done with this then trigger push notification via APN Tester
Download APN Tester from here: https://itunes.apple.com/in/app/apn-tester-free/id626590577?mt=12
Add certificate in APN Tester and hit push button.
and you will get your first push notification
Cheers……:)