Handle Notifications
Declare a callback within your AppDelegate
class to listen for incoming notifications.
As soon as the user interacts with your notification by either swiping or clicking on it, or in case your app is in the foreground, your listener will be invoked and your app can then execute any relevant action in response, such as playing a sound, displaying the relevant storyboard, making a request, etc.
Please select your iOS project type for the appropriate instructions:
Add the following within the didFinishLaunchingWithOptions
method of AppDelegate.swift
, after pushy.register()
:
// Enable in-app notification banners (iOS 10+)
pushy.toggleInAppBanner(true)
// Handle incoming notifications
pushy.setNotificationHandler({ (data, completionHandler) in
// Print notification payload
print("Received notification: \(data)")
// Show an alert dialog
let alert = UIAlertController(title: "Incoming Notification", message: data["message"] as? String, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
// Reset iOS badge number (and clear all app notifications)
UIApplication.shared.applicationIconBadgeNumber = 0
// Call this completion handler when you finish processing
// the notification (after any asynchronous operations, if applicable)
completionHandler(UIBackgroundFetchResult.newData)
})
Add the following within the didFinishLaunchingWithOptions
method of AppDelegate.m
, after [pushy register]
:
// Enable in-app notification banners (iOS 10+)
[pushy toggleInAppBanner:true];
// Handle push notifications
[pushy setNotificationHandler:^(NSDictionary *data, void (^completionHandler)(UIBackgroundFetchResult)) {
// Print notification payload
NSLog(@"Received notification: %@", data);
// Show an alert dialog
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Incoming Notification"
message:[data objectForKey:@"message"]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
// Reset iOS badge number (and clear all app notifications)
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
// Call this completion handler when you finish processing
// the notification (after fetching background data, if applicable)
completionHandler(UIBackgroundFetchResultNewData);
}];
Listen for Notification Click
Optionally add the following within the didFinishLaunchingWithOptions
method of AppDelegate.swift
, after pushy.setNotificationHandler()
, to listen for when the user taps your notifications:
// Handle notification tap event
pushy.setNotificationClickListener({ (data) in
// Show an alert dialog
let alert = UIAlertController(title: "Notification Click", message: data["message"] as? String, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
// Navigate the user to another page or
// execute other logic on notification click
})
Optionally add the following in the didFinishLaunchingWithOptions
method of AppDelegate.m
, after [pushy setNotificationHandler]
, to listen for when the user taps your notifications:
// Handle notification tap event
[pushy setNotificationClickListener:^(NSDictionary *data) {
// Show an alert dialog
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Notification Click"
message:[data objectForKey:@"message"]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
// Navigate the user to another page or
// execute other logic on notification click
}];
Parse Notification Data
Any payload data that you send with your push notifications is made available to your app via the data
parameter of the setNotificationHandler
callback.
If you were to send a push notification with the following data
payload:
{"id": 1, "message": "Hello World!"}
Then you'd be able to retrieve these values in your setNotificationHandler
callback like so:
// Safely unwrap "id" parameter (Int)
if let id = data["id"] as? Int {
print("id: \(id)")
}
// Safely unwrap "message" parameter (String)
if let message = data["message"] as? String {
print("message: \(message)")
}
// Safely extract payload data (nil if missing)
NSNumber* idValue = [data objectForKey:@"id"];
NSString* messageValue = [data objectForKey:@"message"];
if (idValue != nil) {
NSLog(@"id: %@", idValue);
}
if (messageValue != nil) {
NSLog(@"message: %@", messageValue);
}
Note: Unlike GCM / FCM, we do not stringify your payload data, except if you supply JSON objects or arrays.