We have build an app with the Ionic framework (we're beginners to Ionic etc), and we register the device to our Firebase account so that we can sent push messages.
I've now found a PHP snippet which connects to fcm via cURL. I can set a message and title, perfect.
My problem is that I can't get it to work, it requires something called "registration id's". Everything I find about this makes it seem like this is super easy to find, but I can't.
Where to I find these registration ID's? All I want is to send all registered users a notification.
- I have tried about every code I could find, none work. The most obvious IMO is in the Firebase dashboard->authorisation where I find a list with User Id's (which occur after I've registered my device via the app).
- However, this results in
{"error":"InvalidRegistration"}
. When I remove the 'to' all together, the result it "to" (not as JSON, those two characters only).
- When I send messages from the Firebase console get send almost instantly
- I've done
user.getToken()
which results in quite a long string. This doesn't work either
- I've got a token provided by the FCMPlugin, but thats still not the right registration id
The code I am using can be found here. I've changed 'registration_ids'-> 'to' and the 'android.googleapis.com' to 'fcm.googleapis.com' , I've read somewhere that is the new version. Other that that, the code is identical.
I couldn't get it to work with registration id's, I've tried about every possible value I've encountered in Firebase, FCM, my app, or any token.
I've fixed my problem with the FCMPlugin and using topics, quite simple really.
In the app (simplefied):
In your main controller, follow the steps of FCMPlugin:
FCMPlugin.getToken();
FCMPlugin.subscribeToTopic('topicExample');
In your push code:
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'ASDSADASDASDASd........................' );
$fields = array(
'to' => "/topics/topicExample",
'data' => array(
'Extra value1' => 'Foo',
'Extra value2' => 'Bar'
),
'notification' => array(
"title" => "Example title", //Any value
"body" => "Example content", //Any value
"color" => "#666666",
"sound" => "default", //If you want notification sound
"click_action" => "FCM_PLUGIN_ACTIVITY", // Must be present for Android
"icon" => "fcm_push_icon" // White icon Android resource
)
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
//~ curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
Note:
When you register a user to a topic (the 2nd line in my app code), give it some time. I've read a few hours before a new topic is registered, in my case it worked after an hour (didnt check sooner).