1. Enable project notification capabilities
2. Import LocalzPushSDK into the project Create a Podfile with the following format:
use_frameworks! # Swift only!
target "<Enter Your Target Name>" do
pod 'LocalzPushSDK', :git => 'https://github.com/localz/Localz-Push-iOS-SDK.git'
end
NOTE: Swift files importing Objective-C files require a bridging header:
#import <LocalzPushSDK/LocalzPush.h>
3. Add initalisation to the App Delegate
Objective-C
#import <LocalzPushSDK/LocalzPush.h>
@interface AppDelegate() <LocalzPushDelegate,UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[LocalzPush shared].delegate = self;
[LocalzPush initWithAppId:@"<Enter your app ID here>" appKey:@"<Enter your app key here>" start:true config:nil];
return YES;
}
Swift
class AppDelegate: UIResponder, UIApplicationDelegate, LocalzCNCCustomerSDKManagerDelegate, LocalzPushDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
LocalzPush.shared().delegate = self
LocalzPush.initWithAppId("<Enter your app ID here>", appKey:"<Enter your app key here>", start:true, config:nil)
return true
}
4. Add LocalzPush notification handler methods to App Delegate
LocalzPushSDK automatically manages registration tokens and notifications by forwarding the calls from App Delegate
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[LocalzPush shared] appRegisteredForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[[LocalzPush shared] appRegisteredUserNotificationSettings:notificationSettings];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[LocalzPush shared] appFailedToRegisterForRemoteNotificationsWithError:error];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
[[LocalzPush shared] appReceivedActionWithIdentifier:identifier notification:userInfo applicationState:application.applicationState completionHandler:completionHandler];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[LocalzPush shared] appReceivedRemoteNotification:userInfo applicationState:application.applicationState];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[LocalzPush shared] appReceivedRemoteNotification:userInfo applicationState:application.applicationState fetchCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[[LocalzPush shared] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
[[LocalzPush shared] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
LocalzPush.shared().appRegisteredForRemoteNotifications(withDeviceToken: deviceToken)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
LocalzPush.shared().appRegisteredUserNotificationSettings(notificationSettings)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
LocalzPush.shared().appFailedToRegisterForRemoteNotificationsWithError(error)
}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
LocalzPush.shared().appReceivedAction(withIdentifier: identifier, notification: userInfo, applicationState: application.applicationState, completionHandler: completionHandler)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
LocalzPush.shared().appReceivedRemoteNotification(userInfo, applicationState: application.applicationState)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
LocalzPush.shared().appReceivedRemoteNotification(userInfo, applicationState: application.applicationState, fetchCompletionHandler: completionHandler)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
LocalzPush.shared().userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
LocalzPush.shared().userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
5. Add Rich Notification Handler Target (iOS 10 or newer)
Rich notifications allow requested content to be modified before it is presented to the user, such as taking a https attachment URL and presenting it inside the notification.
NOTE: If using Swift, another bridging header is required for the app extension:
#import <LocalzPushSDK/LocalzPushNotificationExtension.h>
source 'https://github.com/localz/Spotz-iOS-Specs.git'
source 'https://github.com/CocoaPods/Specs.git'
project '<ProjectNameHere>'
target '<ProjectTargetNameHere>' do
pod 'LocalzPushSDK'
end
target '<ProjectExtensionTargetNameHere>' do
pod 'LocalzPushSDK/Extension'
end
Objective-C
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
[[LocalzPushNotificationExtension shared] initWithAppId:@"Enter_Localz_App_ID_Here" appKey:@"Enter_Localz_App_Key_Here" options:nil];
[[LocalzPushNotificationExtension shared] didReceiveNotificationRequest:request withContentHandler:contentHandler];
}
- (void)serviceExtensionTimeWillExpire {
[[LocalzPushNotificationExtension shared] serviceExtensionTimeWillExpire];
}
Swift
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
LocalzPushNotificationExtension.shared().initWithAppId("Enter_Localz_App_ID_Here", appKey:"Enter_Localz_App_Key_Here", options:nil)
LocalzPushNotificationExtension.shared().didReceive(request, withContentHandler: contentHandler)
}
override func serviceExtensionTimeWillExpire() {
LocalzPushNotificationExtension.shared().serviceExtensionTimeWillExpire()
}
6. Start Location services for location push (optional)
Use this to enable requests for the device's coordinates via a push notifications. You will need to prompt user to accept location services permission request.
Objective-C
[[LocalzPush shared] enableLocationServices];
Swift
LocalzPush.shared().enableLocationServices()
You will only need to call this once (during app setup/introduction).
7. Test Push via the console
Login to the Localz Push console to send test notifications. Alternatively you can access the console via our microlocation proximity platform Localz console.
There are 3 types of notification that you can send via the console
Notifications can do the following: display messages, set icon badge numbers, play a sound, or be a silent/background notification. Rich Notifications can do the following: same as above, display an image/gif/video, be recalled, or have delivery even when the app is not running in the background.
7. Push via Localz Push API
See the API documentation for more details.
When a user opens the app for the first time, iOS will prompt the user to accept push notifications. This, however, may not be the desired user experience. The right way for asking for permission is explained in this website. In order to time the prompt at the right moment, you will need to do a couple of things:
1. Stop init method from starting the service
In the init method, set start = false. This will stop the init method from starting the service straight after initialisation is completed.
Objective-C
[LocalzPush initWithAppId:@"<Enter your app ID here>" appKey:@"<Enter your app key here>" start:false config:nil];
Swift
LocalzPush.initWithAppId("<Enter your app ID here>", appKey:"<Enter your client key here>", start:false, config:nil)
2. Start LocalzPush service
To pop up the push notification permission dialog (it will only appear if the user has not excepted the permission previously), call the following method when the user is ready to be prompted. You will only need to call this method once during the lifetime of the app.
Objective-C
[[LocalzPush shared] startLocalzPush];
Swift
LocalzPush.shared().startLocalzPush()
Objective-C
[[LocalzPush shared] isPushNotificationEnabled];
Swift
LocalzPush.shared().isPushNotificationEnabled()
Objective-C
[LocalzPush shared].delegate = self;
Swift
LocalzPush.shared().delegate = self
For bugs, feature requests, or other questions, file an issue.
Copyright 2019 Localz Pty Ltd
link |
Stars: 0 |
Last commit: 3 days ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics