iOS
Send Push Notifications

from client side (ObjC)

11min

Send push notifications from client side in Objective-C

Introduction

This section explains how you can send push notifications through your iOS client with Back4App.

This is how it will look like:

Document image


At any time, you can access the complete Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this quickstart, you need:

Going forward we are going to assume you have completed all steps of the Back4App Push Notifications via Dashboard tutorial, even if you use the iOS Project built with this tutorial that is available at our GitHub repository. You should have basic push notifications working and also be able to send pushes out via the admin console.

1 - Enable Client Push

  1. Go to Back4App Website. log in, find your app and click on Server Settings.
  2. Find the “Core Settings” block and click on SETTINGS. The “Core Settings” block looks like this:
Document image


3. Scroll to the end of the page and click on the EDIT DETAILS button, as shown below:

Document image


4. You will see a checkbox called Allow Push Notification from Client in the end of the edit page, tick that box and click on the SAVE button, as shown below:

Document image


2 - Subscribe your device to the News channel

  1. Assuming you have completed the Back4App Push Notifications via Dashboard tutorial, you will want to modify the completed project from that tutorial or download it from our GitHub repository. First, you will add a channel to your Installation object. You are going to do this by altering the method createInstallationOnParse in your AppDelegate file. Open your project’s AppDelegate.m file and add the following line of code - ‘[currentInstallation setObject:@[@”News1”] forKey:@”channels”];’ - which will set the installation object’s channel array to contain one channel called News.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Store the deviceToken in the current Installation and save it to Parse PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; [currentInstallation setObject:@[@"News"] forKey:@"channels"]; [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { NSLog(@"installation saved!!!"); }else{ NSLog(@"installation save failed %@",error.debugDescription); } }]; }

This will allow you to send a message to everyone who subscribes to the channel called News via cloud code

Make sure your version of didRegisterForRemoteNotificationsWithDeviceToken is the same as the code below.

2. Next, we will add a method to your app delegate to send a push to the News channel everytime the app launches. Open your project’s AppDelegate.m file and the method below and make sure this method is fired off everytime the app launches by calling it from didFinishLaunchingWithOptions.

AppDelegate.swif

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) { configuration.applicationId = @"7Ez7z1DfvGFFAXFi8pjhYBOtTGqeU89eSccLBBVN"; configuration.clientKey = @"fySO7DEPIC39LmWJLvugLMTKdlWsLVOmsSZGksqq"; configuration.server = @"https://parseapi.back4app.com/"; }]]; [self registerForRemoteNotifications]; [self sendPushOnLaunch]; return YES; } - (void)sendPushOnLaunch { PFPush *push = [[PFPush alloc] init]; [push setChannel:@"News"]; [push setMessage:@"Push From Device"]; [push sendPushInBackground]; }

3 - Test that you can send targeted push notifications to yourself via the client

Open your app from the simulator while leaving your physical device closed with the lock screen on.

You should see the pushes appear on your device’s lock screen as soon as the app opens on the simulator.

Document image


Final Thoughts

You should have a firm understanding of how to send pushes from the client.

You can combine it with a pfquery to target users based on some sort of property like age, location, or object id.

Just remember that if client push is enabled it can be exploited and can’t be turned off without restricting all client pushes. It’s recommended that you tick to pushes from Cloud Code, but it’s still good to know.

It’s done!

At this stage, you can send push notifications using Client Push through Back4App!