Push messaging

Add Apple Push Notification service
  1. Enable Remote-Notifications
  2. Create Apple Push Notification Certificate
  3. Upload Apple Push Notification Certificate
  4. Asking Permission to Use Notifications
    1. Request Authorization at Launch Time
  5. Handle remote notification
  6. Rich push notifications (optional)
    1. Create Notification Service Extension
    2. Update service extension classes
  7. Tracking opened notifications
  8. Next

Local and remote notifications get the user’s attention by displaying an alert, badging your app’s icon, or playing sounds. These interactions occur when your app isn’t running or is in the background. They let users know that your app has relevant information for them to view. Push messaging should be integrated using Apple Push Notification service.

Woosmap Geofencing SDK uses iOS regionMonitoring to send location based notifications. Regions are a shared system resource, and the total number of regions available systemwide is limited. For this reason, Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app. WoosmapNow is using a fair count of regions. Beware of this information if your app is already using this iOS feature.

Enable Remote-Notifications

Woosmap uses background modes to track when push messages are received on a user’s device before the message is opened. To enable push messaging, select your project from the Project Navigator, choose your application’s target, open the Capabilities tab, and within Background Modes, add tick the Remote notifications checkbox. You can also specify this key directly through your Info.plist as follow:

<key>UIBackgroundModes</key>
<array>
    <string>remote-notifications</string>
</array>

If you already support push notifications in your app, skip to the section below about uploading your push certificate.

Create Apple Push Notification Certificate

Log in to the Apple Developer console and go to Certificates, Identifiers & Profiles.

Because Apple treats development and production as separate instances, we strongly suggest to create separate projects based on intended use, such as one for your development environment and one for the production environment.

Upload Apple Push Notification Certificate

To allow Woosmap to send push messages on your behalf, you must provide Woosmap with the certificate you generated.

Then, you’ll have to send us your certificate as well as the password of certificate to let us import it for your desired project. Please, use the contact form if we’re no already in touch.

Asking Permission to Use Notifications

Because the user might consider notification-based interactions disruptive, you must obtain permission to use them.

Request Authorization at Launch Time

Make your authorization request during your app’s launch cycle. In your app’s launch-time code, get the shared UNUserNotificationCenter object and call its requestAuthorization(options:completionHandler:) method, as shown below. Specify all of the interaction types that your app employs. The example requests authorization to display badge, alerts and play sounds.

import WoosmapNow
import UserNotifications

let notificationCenter = UNUserNotificationCenter.current()
// Request permission to display alerts and play sounds.
notificationCenter.requestAuthorization(options: [.alert, .sound]) 
   { (granted, error) in
    UIApplication.shared.registerForRemoteNotifications()
   }

Handle remote notification

Tells the delegate that the app successfully registered with Apple Push Notification service.

For success registration

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
    Now.shared.setRemoteNotificationToken(remoteNotificationToken: deviceToken)
}

And failed registration

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    NSLog("Error on getting remote notification token : \(error.localizedDescription)")
}

Rich push notifications (optional)

Requires iOS 10.0 or above.

If you want rich notifications (Image, subtitles and more…), proceed to the following steps.

Create Notification Service Extension

Update service extension classes

Replace NotificationService.swift with the below code.

import UserNotifications
import WoosmapNowNotification

class NotificationService: WoosmapNowNotification {
}

Woosmap is tagging its Notifications payload with a woosmap key in userInfo’s dict. If your app is already using a NotificationExtension you can filter received payload by handling only those without this flag.

Tracking opened notifications

Woosmap provides a method to notify our backends that the sent notification has been opened by the user. userInfo is the notification payload. It can be provided by the didReceiveRemoteNotification method from AppDelegate.

Now.shared.notificationOpened(userInfo: userInfo)

Beware that this AppDelegate method is called only when the app received a remote notification AND is running (in background or foreground). This means that you should check for your App’s current state. Or it can be provided by the didFinishLaunchingWithOptions if your app wasn’t running.

Now.shared.notificationOpened(userInfo: launchOptions![UIApplicationLaunchOptionsKey.remoteNotification])

Next

Please, refer to our iOS Simple App hosted on GitHub for a sample app.