Push Notification in IOS Using Grails
Hi All,
In my previous blog . I have explained how to send the push notification to Android Device. Now, it’s time to see how we can send push notification to iOS devices.
For sending Push Notification in iOS I have used JavaPNS_2.2.jar .
So before, directly jumping on example we should know that what are prerequisites of sending push notification to iOS device.
- We need to create an application in iPhone Developer Connection Portal. After creating it we need to create a push cretificate from it which is a .p12 file which will be used for sending the push notification to APNS.
- Then we need to install iOS app on our device which will sends sender ID and application ID to APNS server for registration (Generally Done by iOS Developer).
- Upon successful registration APNS server issues registration ID to iOS device.
- After receiving registration ID, the device will send the registration ID to our server.
- Our server will store the registration ID in the database for later usage.
- Whenever a push notification is needed, our server sends a message to APNS server along with the device registration ID (which has been stored earlier in the database).
- After sending the push notification to the APNS server then it is the responsibilty of APNS server to send the push notification to the desired device using registration ID.
So now, in order to use the JavaPNS_2.2 jar we must copy it in lib directory of grials-app and we can also mention the path of our push certificate file in our Config.groovy for easier access.
[java] ios.certificate.location = "${userHome}/PushCertificates.p12" [/java]
How to send push notification
-
For sending the push notification to iOS device we first need to create the Payload by using the PushNotificationPayload class. This class provide us with different methods to modify the payload but in my case I have used only two of them i.e.
- addAlert(String alertMessage)
- addCustomDictionary(String name,String value)
-
After create the Payload we can send push notification to iOS device by using the static payload() method of Push class.
Letβs take an example.
BirthdayReminderJob
[java]
class BirthdayReminderJob {
def userService
def applePushNotificationService
static triggers = {
cron name: ‘birthdayReminder’, cronExpression: "0 20 5 1/1 * ? *"
}
def execute() {
List<User> birthdayUserList = userService.birthdayList()
if (birthdayUserList) {
applePushNotificationService.sendPushNotification(birthdayUserList)
}
}
}
[/java]
ApplePushNotificationService
[java]
class ApplePushNotificationService {
def grailsApplication
def mobileAppUserService
def mobileUtilService
void sendPushNotification(List birthdayUserList) {
List tokenList = mobileAppUserService.getTokenList(MobilePlatform.IOS)
Map birthDayMap = mobileUtilService.getNotification(birthdayUserList,tokenList)
if (birthDayMap) {
sendPushNotificationToIOSUser(birthDayMap, tokenList)
}
}
def sendPushNotificationToIOSUser(Map birthDayMap, List tokenList) {
PushNotificationPayload payload = PushNotificationPayload.complex()
payload.addAlert(birthDayMap.title)
payload.addCustomDictionary("birthDayMap", birthDayMap.isBirthday)
String password = ""
String filePath = grailsApplication.config.ios.certificate.location
tokenList.each { String token ->
try {
PushedNotifications pushedNotifications = Push.payload(payload, filePath, password, false, token)
} catch (Exception exception) {
log.info(exception)
}
}
}
}
[/java]
Hope this helps !! π π