IOS
Integration with iOS push notification service

General information

To enable Push Notifications, please perform the following actions:
    1.
    Add the application to your space in devtodev system
    2.
    Generate Developer or Production Certificate for the application and get Private key file (.p12) on its basis
    3.
    Submit the data to the application settings in devtodev system
    4.
    Integrate devtodev SDK to the application (see the “SDK integration” division to learn more about integrating and initializing devtodev SDK)
    5.
    Add several lines of the code to switch in the push notification to the SDK
    6.
    Create a campaign for sending push notifications in “Push Notifications” section

Enabling Push Notifications and certificate generation

Enabling Push Notifications

First enable push notifications in your Xcode project.
The library provides support for iOS 10 notification attachments, such as images, animated gifs, and video. In order to take advantage of this functionality, you will need to create a notification service extension alongside your main application.
Create a new iOS target in Xcode (File -> New -> Target) and select the Notification Service Extension type.
​In Member Center, the Push Notifications service will appear as Configurable (not Enabled) until you create a client SSL certificate.
Drag the devtodevAppExtensions.framework into your app project.
Modify your extension.
    1.
    Delete all dummy source code for your new extension
    2.
    Inherit from DTDMediaAttachmentExtension in NotificationService
1
//NotificationService.h
2
#import <devtodevAppExtensions/devtodevAppExtensions.h>
3
4
@interface NotificationService : DTDMediaAttachmentExtension
5
6
@end
7
8
//NotificationService.m
9
#import "NotificationService.h"
10
11
@implementation NotificationService
12
// NOTE: Keep this empty implementation to prevent class stripping.
13
@end
Copied!

Adding Capabilities

Use Xcode to enable push notifications in the target’s Capabilities pane:
Enable Background Modes and Remote notifications under the target’s Capabilities section:

Creating a Universal Push Notification Client SSL Certificate

You use Member Center to generate a push notification client SSL certificate that allows your notification server to connect to the APNs. Each App ID is required to have its own client SSL certificate. The client SSL certificate Member Center generates is a universal certificate that allows your app to connect to both the development and production environments.
Only a team agent or admin can generate Apple Push Notification service SSL certificates.
To generate a universal client SSL certificate
    1.
    In Certificates, Identifiers & Profiles, select Certificates.
    2.
    Click the Add button (+) in the upper-right corner.
    3.
    Under Production, select the “Apple Push Notification service SSL (Sandbox & Production)” checkbox, and click Continue.
    4.
    Choose an App ID from the App ID pop-up menu, and click Continue. Choose the explicit App ID that matches your bundle ID.
    5.
    Follow the instructions on the next webpage to create a certificate request on your Mac, and click Continue.
    6.
    Click Choose File.
    7.
    In the dialog that appears, select the certificate request file (with a .certSigningRequest extension), and click Choose.
    8.
    Click Generate.
    9.
    Click Download.

Follow these steps to export the certificate from Apple web-site to the P12-file:

    1.
    Open "Keychain access" application
    2.
    If the certificate hasn't been added to keychain access yet, choose "File" → "Import". Find the certificate file (CER-file) provided by Apple
    3.
    Choose "Keys" section in "Keychain access" application
    4.
    Choose a personal key associated with your iPhone developer certificate. Personal key is identified by open certificate associated with it "iPhone developer: ". Choose "File" → Export objects. Save key as .p12
    5.
    You'll be suggested to create a password which is used when you need to import the key to another computer

Convert the certificate from Apple web-site to the P12-file on Windows OS

Convert Apple certificate file to the PEM-file. Start following command-line operation from bin catalog OpenSSL.
1
openssl x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM
Copied!
Convert personal key from Mac OS keychain to the PEM-key:
1
openssl pkcs12 -nocerts -in mykey.p12 -out mykey.pem
Copied!
Now you are able to create P12-file using PEM-key and iPhone developer certificate:
1
openssl pkcs12 -export -inkey mykey.key -in developer_identity.pem -out iphone_dev.p12
Copied!
If you are using key from Mac OS keychain then choose PEM-version created in the previous step. Otherwise, you can use OpenSSL key for Windows OS.

Upload the certificate to the site

Upload the .p12-file into Integration section of application settings panel (Settings -> Push Notifications):

SDK Integration

After the certificate has been generated you can start to integrate Push SDK into your app.
1. Open the "Capabilities" tab in the XCode. Switch "Push Notifications" and "Background Modes" to ON. In "Background Modes" check "Remote notifications".
2. Add the following strings to the AppDelegate class:
1
#import "AppDelegate.h"
2
#import <devtodev/devtodev.h>
3
4
@implementation AppDelegate
5
6
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
7
*)launchOptions {
8
9
[DevToDev initWithKey:@“APP_KEY” andSecretKey:@“SECRET_KEY”];
10
11
dispatch_async(dispatch_get_main_queue(), ^{
12
[DevToDev pushManager].delegate = self;
13
[DevToDev pushManager].pushNotificationsOptions = (DTDNotificationOptionAlert | DTDNotificationOptionBadge | DTDNotificationOptionSound);
14
[DevToDev pushManager].pushNotificationsEnabled = YES;
15
});
16
17
// Your code...
18
return YES;
19
}
20
21
22
/**
23
* @brief Device received a token and was successfully subscribed for notifications
24
* @param deviceToken device push token
25
**/
26
-(void) didRegisterForRemoteNotificationsWithDeviceToken: (NSString *) deviceToken {
27
28
}
29
30
/**
31
* @brief Error occured while registering for push notifications
32
* @param error text
33
**/
34
-(void) didFailToRegisterForRemoteNotificationsWithError: (NSError *) error {
35
36
}
37
38
/**
39
* @brief Push notification has been received by the device
40
* @param notification data
41
**/
42
-(void) didReceiveRemoteNotification: (NSDictionary *) notification {
43
44
}
45
46
/**
47
* @brief Push notification has been opened
48
* @param pushMessage body
49
* @param actionButton button that was clicked
50
**/
51
-(void) didOpenRemoteNotification: (DTDPushMessage *) pushMessage withAction: (DTDActionButton *) actionButton {
52
53
}
54
55
/**
56
* @brief Push notification receive response
57
* Note: This method is relevant only for iOS 10 and above.
58
* @param response notification
59
**/
60
-(void) didReceiveNotificationResponse: (DTDNotificationResponse *)response {
61
62
}
Copied!
If you are using iOS 12 or above, you can specify custom options for notification settings:
1
typedef NS_OPTIONS(NSUInteger, DTDNotificationOptions) {
2
DTDNotificationOptionBadge = (1 << 0),
3
DTDNotificationOptionSound = (1 << 1),
4
DTDNotificationOptionAlert = (1 << 2),
5
DTDNotificationOptionCarPlay = (1 << 3),
6
DTDNotificationOptionCriticalAlert = (1 << 4),
7
DTDNotificationOptionProvidesAppNotificationSettings = (1 << 5),
8
DTDNotificationOptionProvisional = (1 << 6)
9
};
Copied!
3. Open "Build Settings", find the parameter "Other Linker Flags", then add 2 flags: -ObjC and -lc++
4. Compile and run the app. You will need a device because the simulator does not support push notifications. Xcode will automatically choose a new provisioning profile. If an error occurred during the launch make sure that there is a correct profile set in the Code Signing Identity. You'll be asked to confirm push notifications. An app will request permission only once, if user confirms it - notifications will be accepted otherwise he won't get any push messages from your app. Users can change it in device settings.

Creating a new push notification in devtodev interface

1. Open PUSH NOTIFICATIONS section and click on the "Add new campaign" button
2. Fill in the campaign name
You can create a campaign only after at least one push token comes from devtodev SDK integrated into your application. Otherwise, the app will not be displayed in the list.
3. Choose a user group to send a message. You can choose an existing segment or create a new one
4. Enter notification details
5. Test push notification (or skip this step)
6. Confirm push gateway
7. Schedule the delivery
8. That's it!
Last modified 1mo ago