Date post: | 21-Jan-2018 |
Category: |
Technology |
Upload: | cumulations-technologies |
View: | 983 times |
Download: | 0 times |
Push NotificationUsingFirebase Cloud Messaging
Topics
Push Notification
FCM
FCM implementationApplication/Client side
AndroidiOS
Server side
Push Notification
Push Notification is an message which pop up in the device. When the Application is Foreground or background.
FCMFCM is cross platform Messaging solution for reliably deliver message withno cost.
FCM Formerly known as GCM.
It is supported For Android, iOS and Web platforms.
Implementation in Android
Create a Firebase project for Application in the Firebase consoleFetch the SenderId and Server key
Application/Client Side Implementation
Add FCM Library in Project
dependencies {
compile 'com.google.firebase:firebase-messaging:+'
}
Fetch FCM client token and send to App server
FirebaseInstanceId.getInstance().getToken();
Implementation in AndroidImplement a service to handle Notification.
Class MyFirebaseMessagingService extends FirebaseMessagingService
{
onMessageReceived(RemoteMessage remoteMessage)
}
Notification Json format{
noification:{
title:
text:
},
Data:{
//custom key value pair
}
}
Application/Client side ImplementationImplementation in IOS
Requirement.
Steps for enabling push notification in iOS app.
Steps for enabling fcm in iOS app.
Notification handling.
Requirement
● Developer Id
● Mac OS
● Xcode 7 > (For fcm implementation)
● iOS device
Implementation in iOS
Steps for enabling push notification in iOS
● Enable push notification in app capabilities
● Setting APNs
○ Create ssl certificate using apple id
○ Create provisioning profile
○ And download and install it
○ And export that installed certificate to get p12
Implementation in IOS
For Fcm
● Import fcm using pod
● Download plist file
● Copy plist file to project
Implementation in IOS
● Register for remote notification
● Access registered token
● Monitor the token refresh
Steps for enabling push notification in iOS
Implementation in iOS
Handling and Types
Notification type
Remote notification
From server need notification token and pem file
Need fcm registration token with fcm
Local notification
Can be scheduled
Example- alarm
Implementation in iOS
Workflow
Implementation in iOS
Components
Requirement
Protocol and Method
Format
Server Side Implementation
● Components○ FCM connection servers: provided by Google. These servers take
messages from an app server and send them to a client app running on a device
○ App server: Sends notifications to the client app via FCM connection servers
Server Side Implementation
● Requirement○ End point: https://fcm.googleapis.com/fcm/send○ Server Key: AAAA3NfOkRw:APA91bE5d-p2a4uP1ah-
EysCnUpLjS9px3Luy-mj-7qclUIRW-i18gQHvvw7-mhFuSoZTI3TVrwbi-Q8UHq2K--6IxDJ4UGiKzATRBwf1soBb6GNcJ6sEHrC5OeUegJfNhzIlh-i46X_LP6Hh48AxZhJePQ1gw_ZSg
○ Registered Ids: ctin8xiuslQ:APA91bEX4-YjyqmfNozCd2qYEWxDVU9O1j5eKI8Ez4pWzF-ytIAyExGtb02G-wHfPToh1dYfmkDJdsJWWnOu1bgsyPQqbIntatBMULP_20OdTSVw1PxTgEbS5qoniwfM6H3jyTpQecBq
Server Side Implementation
● Protocol○ There are two protocols available: HTTP and XMPP○ The difference b/w HTTP and XMPP messaging is as follows:
■ Upstream/Downstream messages● HTTP: supports only downstream up to 4kb of data● XMPP: supports both up to 4kb of data
■ Messaging● HTTP: synchronous● XMPP: asynchronous, the XMPP connection server sends acknowledgment or failure
notifications (in the form of special ACK and NACK JSON-encoded XMPP messages) ■ JSON
● HTTP: supported● XMPP: supported (JSON message is encapsulated in XMPP messages)
■ Plain Text● HTTP: supported● XMPP: not supported
■ Multicast downstream send to multiple registration tokens● HTTP: supported● XMPP: not supported
● App server makes a POST (HTTP) request to the FCM connection server (curl is used for the same in php)
Server Side Implementation
● Request Format (HTTP) - Downstream message○ header must contain following keys will appripriate values:
■ Authorization: key=SERVER_KEY■ Content-Type: application/json (JSON) or application/x-www-form-urlencoded;charset=UTF-8 (plain text)
If content type is not provided the format will be assumed to be plain text○ Body must contain the following:
■ registration_ids (array)/to (string) - device registration tokens■ notification (object) - predefined, user-visible key-value pairs of the notification payload■ data (object) - custom key-value pairs of message payload
● Response Format (HTTP) - Downstream message○ Status: 200 on success/ non 200 status (4xx, 5xx) on failure○ Body
■ multicast_id - Unique ID (number) identifying the multicast message■ success - Number of messages that were processed without an error■ failure - Number of messages that could not be processed■ canonical_ids - Number of results that contain a canonical registration token■ results (array of objects)
● message_id - unique ID for each successfully processed message● registration_id - canonical registration token for the client app to which the message was processed
and sent to● error - error that occurred when processing the message for the recipient
Server Side Implementation
CURL Request Code:
Server Side Implementation
Response:
Server Side Implementation
Thank You