3

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.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • 1
    Hi, could you post the code where you register the device with FCM, The registration id's are the device tokens that the google server sends back after your device is successfully registered with FCM. User.getToken returns the JWT token, that is used to sign in the user(Firebase Auth), it is not related to FCM(Firebase Cloud Messaging) – Akil Nov 11 '16 at 11:27
  • I dont think I register to FCM, I'm currently checking this out: https://github.com/fechanique/cordova-plugin-fcm , but I'm not there yet – Martijn Nov 11 '16 at 11:59
  • 1
    If your using that plugin, you can get the token with the FCMPlugin.getToken function, The work flow is simply to first register with firebase, get the token , use that token in the 'to' parameter to send a notification, lastly handle the incoming notification. – Akil Nov 11 '16 at 12:07
  • Haha thanks, I'm doing my best, but it's the first time so progress isn't fast. – Martijn Nov 11 '16 at 12:26
  • I get a token via `FCMPlugin.getToken()`, but it still says `InvalidRegistration`. – Martijn Nov 11 '16 at 12:47
  • Turns out I could get it to work when I created a topic for the users. If I've got time, I'll add an answer after the weekend. – Martijn Nov 11 '16 at 14:54

1 Answers1

2

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).

Martijn
  • 15,791
  • 4
  • 36
  • 68