Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up push notifications in Swift

I am trying to set up a push notification system for my application. I have a server and a developer license to set up the push notification service.

I am currently running my app in Swift. I would like to be able to send the notifications remotely from my server. How can I do this?

like image 833
BlakeH Avatar asked Jul 22 '14 22:07

BlakeH


People also ask

How do I set up push notifications on iOS?

Setting up Push Notifications in iOS. Go back to the iOS project you created earlier and select the main target. Under the Signing & Capabilities tab, click on the + Capability button, then select Push Notifications. This will enable your app to receive push notifications from OneSignal.

What is APNs in Swift?

Apple Push Notification service (APNs) must know the address of a user's device before it can send notifications to that device. This address takes the form of a device token unique to both the device and your app.


Video Answer


2 Answers

Swift 2:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() 
like image 161
Adam Waite Avatar answered Sep 24 '22 03:09

Adam Waite


While the answer is given well to handle push notification, still I believe to share integrated complete case at once to ease:

To Register Application for APNS, (Include the following code in didFinishLaunchingWithOptions method inside AppDelegate.swift)


IOS 9

var settings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() 

After IOS 10

Introduced UserNotifications framework:

Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate.swift


To Register Application for APNS

let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in      // If granted comes true you can enabled features based on authorization.     guard granted else { return }      application.registerForRemoteNotifications() } 

This will call following delegate method

func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { //send this device token to server }  //Called if unable to register for APNS. func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {  println(error)  } 

On Receiving notification following delegate will call:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {      println("Recived: \(userInfo)")    //Parsing userinfo:    var temp : NSDictionary = userInfo    if let info = userInfo["aps"] as? Dictionary<String, AnyObject>              {                 var alertMsg = info["alert"] as! String                 var alert: UIAlertView!                 alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")                 alert.show()             } } 

To be identify the permission given we can use:

UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in          switch setttings.soundSetting{         case .enabled:             print("enabled sound")          case .disabled:             print("not allowed notifications")          case .notSupported:             print("something went wrong here")         }     } 

So the checklist of APNS:

  • Create AppId allowed with Push Notification
  • Create SSL certificate with valid certificate and app id
  • Create Provisioning profile with same certificate and make sure to add device in case of sandboxing(development provisioning)

Note: That will be good if Create Provisioning profile after SSL Certificate.

With Code:

  • Register app for push notification
  • Handle didRegisterForRemoteNotificationsWithDeviceToken method
  • Set targets> Capability> background modes> Remote Notification
  • Handle didReceiveRemoteNotification
like image 26
Arvind Avatar answered Sep 25 '22 03:09

Arvind