Chapter    17

Promotion

Push notification marketing is frequently used to promote discounts, deals, and new products. In general, creating the feature of push notification marketing can be quite complicated. A common use case is that once an app receives a push notification and a user taps it, the app will bring up the product page directly or a promotion section within this app. It will take quite an effort to establish features like this. However, we also have seen a use case like this: The user’s device received a push notification; the user opens the app and then sees a promotion page. This page acts like a flyer and has content similar to “Check out our new products.” Or “10% off for X’mas.” Since the flyer page is an overlay on top of the main app, it can be dismissed with a tap. In this chapter, I will show you how to implement this behavior by using Parse’s easy-to-use push notification feature.

Keep in mind though, to see this in action, you need to configure your app to receive push notifications with Apple Developer Portal and Parse, as introduced in Chapter 4.

Model

Create a new PFObject subclass for this promotion model and name it “EMABPromotion.” It has two properties: one is for the promotion content, and the other is for the promotion image. Here is the header file:

Next, register a new NSString constant kPromotion with the EMABConstants class. Also register the new EMABPromotion model with the AppDelegate class. Here is the EMABPromotion implementation file:

View

The promotion flyer view is pretty simple. Use a UILabel to show the content and a PFImageView to show the image, just like a flyer. Figure 17-1 shows what it looks like.

9781484213186_Fig17-01.jpg

Figure 17-1. This is an example of a promotion flyer

Controller

Create a new UIViewController subclass, and name it “EMABPromotionViewController.” Here is the header file for this controller:

Here is the implementation file, and it’s pretty basic. Once you have an instance of EMABPromotion, connect this model with the view. Let the content label show the promotion’s content, and let the image view show the promotion’s image.

@implementation EMABPromotionViewController

-(void)setPromotion:(EMABPromotion *)promotion
{
    if (_promotion != promotion) {
        _promotion = promotion;

        [self configureView];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureView];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onDone:)];
}

-(void)configureView {
    self.contentLabel.text = self.promotion.content;
    if (self.promotion.image) {
        self.imageView.file = self.promotion.image;
        [self.imageView loadInBackground];
    }
}

-(void)onDone:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end

In the end, use a bar button item to dismiss the view controller when a user taps it.

Next, think about how to show the flyer view controller. The content of the flyer is sent with a push notification. The screen is shown when a user taps the notification. You need to move over to the EMABAppDelegate.

You know -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will be used in response to the user’s tapping on a notification message, given that the app is in the background or has not added a helper method -(void)handlePromotion:(NSDictionary *)options.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     // Register EMABPromotion class
    [EMABPromotion registerSubclass];

    // Handle Promotion
    [self handlePromotion:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];

    return YES;
}

-(void)handlePromotion:(NSDictionary *)notificationPayload {
     // Create a pointer to the Photo object
     NSString *objectId = [notificationPayload objectForKey:@"p"];
     EMABPromotion *promotion = (EMABPromotion *)[PFObject objectWithoutDataWithClassName:kPromotion
                                                             objectId:objectId];
     [promotion fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        // Show promotion view controller
        if (!error) {
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            EMABPromotionViewController *viewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"EMABPromotionViewController"];
            [self.window.rootViewController presentViewController:viewController animated:YES completion:nil];
        }
    }];
}

In this helper method, check for a promotion notification message. If yes, construct an EMABPromotion instance promotion based on one of the keys in the message content, the objectId. You need to do a little bit of remote fetching to get the entire content. Once the fetching is done, bring up the EMABPromotionViewController.

With Parse, it’s just that easy.

Summary

In this chapter, I have introduced you to using push notifications to do a little bit of marketing. Basically, you implement a flyer page while a user taps a promotion push notification, then your app will query and load the promotion content from the Parse back-end server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.18.101