PHP => FCM Push notification tutorial for 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.
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.
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.
// // PHP SERVER SIDE CODE BELOW //
// generated via the cordova phonegap-plugin-push using "senderID" (found in FCM App Console) // this was generated from my phone and outputted via a console.log() in the function that calls the plugin // my phone, using my FCM senderID, to generate the following registrationId $singleID = 'eEvFbrtfRMA:APA91bFoT2XFPeM5bLQdsa8-HpVbOIllzgITD8gL9wohZBg9U.............mNYTUewd8pjBtoywd' ; $registrationIDs = array( 'eEvFbrtfRMA:APA91bFoT2XFPeM5bLQdsa8-HpVbOIllzgITD8gL9wohZBg9U.............mNYTUewd8pjBtoywd', 'eEvFbrtfRMA:APA91bFoT2XFPeM5bLQdsa8-HpVbOIllzgITD8gL9wohZBg9U.............mNYTUewd8pjBtoywd' 'eEvFbrtfRMA:APA91bFoT2XFPeM5bLQdsa8-HpVbOIllzgITD8gL9wohZBg9U.............mNYTUewd8pjBtoywd' ) ;// prep the bundle // to see all the options for FCM to/notification payload: // https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
// 'vibrate' available in GCM, but not in FCM
$fcmMsg = array(
'body' => '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"; ?>