Push Notification helps developer in sending event or alert to app users even when they are not logged into app. App42 uses APNS (Apple Push Notification Service) to send Push Notification on iOS devices.
Configuring settings for APNS, see here
We have encapsulated all the code written below in a sample project. Download the Sample from our GitHub Repo and unzip it.
Open Xcode project and go to Constants.h file in which you need to pass the required information like API KEY & SECRET KEY which you have received after the success of app creation from AppHQ Console.
Xcode settings to enable iOS Application for Push Notification:
In order to configure your app for Push Notification, we need to modify Bundle Identifier and Code Signing as per the Provisional Profile we installed earlier.
Open your Xcode project and select info.plist file under Supporting Files in the left-hand-side menu. Change the Bundle Identifier same as App ID’s Bundle Identifier , for me it is com.shephertz.app42pushapp.
Select the project file in the left-hand-side menu and then select your project under PROJECT section. Then select Build Settings and search for the Code Signing .
Select the Provisioning Profile that you installed earlier for all values under this heading.
Select Code Signing Identity as automatic for all values under this heading.
Select your project under TARGETS section.Then again select Build Settings and search for the Code Signing. Repeat the above process again to make sure all the values matches with new provisioning profile.
1.2 Registering the Device for Push:
Navigate to the AppDelegate.m class and change the application:didFinishLaunchingWithOptions: method to look like this:
The above code tells the OS that this App wants to receive push notifications.
Now add this delegate method in the AppDelegate.m class that will be called in case of success and failure of the registration.
When your App registers for remote (push) notifications, it tries to obtain a device token. A device token is the address to which a push notification will be delivered.
1.3 Registering the device to App42 Push Server:
After getting the device token you need to register your device with the App42 Server to set up a connection to APNS to send the push notification to this device token. Change the delegate method application:didRegisterForRemoteNotificationsWithDeviceToken: in the AppDelegate.m class to look like this:
1.4 Sending Push Notification:
The push notification can be sent from the iOS Application as well as from the AppHQ console.
1.4.1 Sending Push from your iOS Application:
Now to send a push notification, call the following method in a commonly used class in your project so that you can call this whenever you want to:
Note:- To increment badge automatically when message received, send “increment” as a value for key “badge” in the above message dictionary.
In the sample, we have written this method inside ViewController.m.
Above method implements App42 PushNotificationService API sendPushMessageToUser to send a Push message to a specific user. Likewise, App42 PushNotificationService has several APIs for sending push messages.
1.4.2 Sending Push from AppHQ Console:
To send Push messages from AppHQ Console, Login to AppHQ Console and select Users under Push Notification in AppHQ Menu.
Select your App42 App name from drop down menu under Users section.
Select the user name to whom you want to send Push message.
Click Send Push.
Type your message in the Message section in the Send Push window and Click Send and you are done.
For the detail delivery reports you can see the Push Logs by selecting Push Logs option from the left-side menu and then the respective App42 App from the drop down under Push Logs.
You can also send push messages to All your users by selecting Send Push To All instead of Send Push.
1.5 Handling Push Notifications When Received:
If you want to take any actions when you receive push notification then you need to add the following delegate method in the AppDelegate.m class:
1.6 Sending Message from Server Side/other SDKs with Sound and Badge Effect
If you want to send push message with sound and badge effect on iOS device, you have to pass JSON string in below format using JAVA SDK. Similarly , you can use other server side SDK to achieve this.
You can also do it from AppHQ by putting this JSON format in message text area for sending message to iOS device with sound and badge effect.
1.7 Sending Message to Channel
App42 Push notification also supports channel subscription model where user can subscribe on channel of his interest to receive the notification. You can send message to channel which will deliver message to all users who are subscribed to that channel. A channel can be created from AppHQ console. Go to Push Notification -> Click on Channels -> Select your App -> Click on add channel button
Once channel is created you can ask user for subscription on the channel. Below is the code snippet for the same
1.8 Scheduling Message to User
You can also schedule message to your app user on specified time from AppHQ console. To do this, go to Push Notification -> Click on Users -> Select your app -> select target users/device and click on schedule push button. Now type your message in pop up and pass your time to schedule the message.
1.9 Scheduling message to Channel
Message scheduling on channel can be done in similar way. Go to Push Notification -> Click on Channels -> Select your app -> select target channel and click on schedule push button. Now type your message in pop up and pass your time to schedule the message.
Push notification goes from App42 server to APNS and then device. Delivery of Push notification is not guaranteed from APNS to device and it is stated as best effort. Once it is delivered on user device, there might be a chance that user clears it without actually opening the message. Using App42 Push Analytics, you can track how many push notification was sent from your side, and how many were opened by the user. You can see these analytics from AppHQ console which will give a better insight of push notification campaign. If you want to track push message read/opened event you have to simply put following snippet after Message is clicked and lands to your app. This will enable tracking of push message and analytics can be seen from AppHQ console.
Once your device is registered for Push Notification you are ready to send multilingual(UTF-8) push message to your app user using following code snippet from any App42 SDK (Android/Java/WP etc)
For more details of Push Notification Documentation