Mobile

Push messaging

Add Apple Push Notification service

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 Mobile 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.

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.

  • Select Identifiers > App IDs and then select your app.
  • Click Edit to update the app settings.
  • If not already enabled, tick the Push Notifications service checkbox to enable it.
  • If you already have a certificate created which you can use, download it and proceed to the next step. If not, click Create Certificate….
  • Follow the instructions on the next webpage to create a certificate request on your Mac, and click Continue.
  • On the Generate your certificate page, click Choose File and choose the certificate request file you just created (with a .certSigningRequest extension) and then click Continue.
  • Download the generated certificate to your Mac and then open the .cer file to install it in Keychain Access.

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.

  • Launch Keychain Access on your Mac.
  • In the Category section, select My Certificates.
  • Find the certificate you generated and expand it to disclose its contents. You’ll see both a certificate and a private key.
  • Select both the certificate and the key, and select File > Export Items… from the menu and save it as .pem format.

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

  • In Xcode, navigate to File > New > Target and select Notification Service Extension.
  • Click Next, fill out the Product Name as NotificationService, and click Finish.
  • Click Activate on the prompt shown to activate the service extension. Xcode will now create a new top level folder in your project with the name NotificationService.

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.