how to setup and use Googles Firebase push notification API for both Android and iOS
Below is a full tutorial on how to setup and use Googles Firebase push notification API for both Android and iOS. It is based on this
earlier implementation of Googles GCM method: https://gist.github.com/prime31/5675017 – FCM is the new method and GCM will eventually be
retired.
## THE BELOW METHOD IS THE NEWER FCM METHOD:
Register your app in the FCM Console: https://console.firebase.google.com (add project)
`NOTE: Once your register your app in the FCM console and retrieve your Server Key and SenderID, it might take 12 to 24 hours
for the IDs to propagate to the FCM messaging servers. I experienced this, minutes after registering my code worked but phone
never received the messages…10 hours later…no changes to my code…phone suddenly started getting the messages. I assume
it was a propagation issue.`
## REGISTERING YOUR APPLE APNs KEY in GOOGLE’S FCM CONSOLE:
`You iOS app is now registered with Googles FCM, Google FCM can now send/relay messages to iPhones/iPads
The only remaining step is to have your client iOS/iPhone App retrieve and send a proper registrationID to your server
side script (below). In my case, the same Cordova phonegap-plugin-push plugin extracts the Apple registrationID too.`
`NOTE: If you are integrating the Firebase SDK into a phonegap/cordova project, when your App launches it will try to connect to Firebase Crashlytics automatically, you will need to setup Crashlytics in our FCM App Project. If you are using Fabric in our current project you will need to link your Fabric account to your FCM Project App or you will get [Fabric] errors in the app launch. How to link Fabric to FCM Crashlytics: https://proandroiddev.com/migrating-crashlytics-to-the-firebase-console-5e05b6ff8c12`
// I migrated my server side code to a php-fcm class project, making server side FCM notification mgmt MUCH easier and cleaner.
// The guy who wrote the original no longer supports it so I expanded it out to include a lot more functionality and plan
// on adding more.
//
// NEW PHP SERVER SIDE CODE
// https://github.com/rolinger/php-fcm
addRecipient($deviceId)
->setTitle(‘Hello from php-fcm!’)
->setBody(‘Notification body’)
->addData(‘key’, ‘value’), // add single key/value pair
->addDataArray($myArray); // add preset array of key/value// Send the notification to the Firebase servers for further handling.
$client->send($notification);
//
// OLD PHP SERVER SIDE CODE BELOW –
//
‘here is a message. message’,
‘title’ => ‘This is title #1’,
‘sound’ => “default”,
‘color’ => “#203E78”
);
// I haven’t figured ‘color’ out yet.
// On one phone ‘color’ was the background color behind the actual app icon. (ie Samsung Galaxy S5)
// On another phone, it was the color of the app icon. (ie: LG K20 Plush)// ‘to’ => $singleID ; // expecting a single ID
// ‘registration_ids’ => $registrationIDs ; // expects an array of ids
// ‘priority’ => ‘high’ ; // options are normal and high, if not set, defaults to high.
$fcmFields = array(
‘to’ => $singleID,
‘priority’ => ‘high’,
‘notification’ => $fcmMsg
);$headers = array(
‘Authorization: key=’ . API_ACCESS_KEY,
‘Content-Type: application/json’
);$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, ‘https://fcm.googleapis.com/fcm/send’ );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fcmFields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result . “\n\n”;
?>