Push Notifications

This guide will help you to properly configure Push notifications in Apphud.

Integrating Push notifications in your app will let you to use Rules – a powerful feature that lets you increase your app revenue by automatically offering a discount to a user at the specified moment.

Generate Push Notifications Auth Key

Go to the Apple Developer Center, then go to "Keys" page, then create a new key by entering a name and choosing "Apple Push Notifications service (APNs)":

Once created, download the file and move it to the safe place. You will need to upload it to Apphud. Also, copy your team ID somewhere which can be found in your Developer Account Membership page. We will need it later.

Auth Key file name has the following format: AuthKey_[KEY_ID].p8, where KEY_ID is your Key Identifier.

By using auth key, Apphud will be able to send Push notifications to all of your apps within the same account and to both sandbox and production environments. That means, you can use the same auth key file for your another app.

Upload Auth Key to Apphud

Go to Apphud Dashboard and open "App settings", select "Push" tab. There you will see Upload button for APNs auth key file. Just upload your Key file there. Also enter your Team ID and make sure key identifier is also entered:

Set up Push Notifications in iOS app

Make sure that Push Notifications are turned on in "Capabilities" section of your app target:

Let's write some code in your AppDelegate. First, register for notifications:

Swift
Objective-C
Swift
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Apphud.start(apiKey: YOUR_API_KEY)
registerForNotifications()
//... the rest of your code
}
func registerForNotifications(){
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
// handle if needed
}
UIApplication.shared.registerForRemoteNotifications()
}
Objective-C
#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[Apphud startWithApiKey:@"APPHUD_API_KEY" userID:nil];
[self registerForNotifications];
}
- (void)registerForNotifications{
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
// handle if needed
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}

Then you will need to submit device token to Apphud:

Swift
Objective-C
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Apphud.submitPushNotificationsToken(token: deviceToken, callback: nil)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// error occurred. Probably you have signing issues or push notifications capabilities are // turned off
}
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[Apphud submitPushNotificationsTokenWithToken:deviceToken callback:nil];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
// error occurred. Probably you have signing issues or push notifications capabilities are turned off
}

We should handle incoming Push notification payload after. This is done with two methods: one handles payload when app is in background or not launched, another – handles payload when app is running.

Swift
Objective-C
Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
Apphud.handlePushNotification(apsInfo: response.notification.request.content.userInfo)
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
Apphud.handlePushNotification(apsInfo: notification.request.content.userInfo)
completionHandler([]) // return empty array to skip showing notification banner
}
Objective-C
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
[Apphud handlePushNotificationWithApsInfo:response.notification.request.content.userInfo];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[Apphud handlePushNotificationWithApsInfo:notification.request.content.userInfo];
}

That's it. Run the app and make sure that deviceToken is successfully sent to Apphud.

Now you are ready to receive Push notifications.

Troubleshooting

Push notification doesn't arrive.

  1. Double check that Auth Key is uploaded to App Settings in Apphud and both Key ID and Team ID values are set.

  2. Make sure that Apphud receives device tokens from your iOS app, i.e. double check that Apphud.submitPushNotificationsToken(token: deviceToken, callback: {_ in}) method is being called.

  3. View your Apphud User page – it should contain push token value.

Push notification arrives but it's not being handled.

  1. Make sure that Apphud.handlePushNotification(apsInfo: userInfo) method is being called.

  2. Make sure that UNUserNotificationCenterDelegate is correctly set.

  3. If you are using OneSignal or custom UINotificationServiceExtension , debug in Xcode and make sure that Apphud.handlePushNotification(apsInfo: aps) method is called.

Using both OneSignal and Apphud Push Notifications

Please see this guide.