Reputation: 23873
I'm investigating the possibility of sending messages to a project using Firebase Cloud Messaging migrated from an old Google Cloud Messaging app.
The old app only sends messages to an Android device from a php webpage using CURL and the app's only API key for GCM which is on my server.
Thus each android device only needs to register with code such as:
mGcm = GoogleCloudMessaging.getInstance(mContext);
mRegid = mGcm.register(PROJECT_ID);
to let the Google server make each device individually addressable. (The regids are stored on my MySql database on my server, so I know which device to address.
The Android app only needs to know the project number (PROJECT_ID in the above code). The app can have any package name.
It appears to me that a Firebase project must be associated with a package name.
My question is :
Is it possible to replicate this functionality with Firebase? I.e Can I create an app with any package name and get a regid (or now: token) in a similar manner to the GCM project? .
Upvotes: 3
Views: 1037
Reputation: 9225
Yes, with FCM the generation of an Instance ID token (device id) is still based on the Project ID, and you can still send to that device using the IID token and a valid API key from your project.
Most of the benefits of using Firebase would be lost however if you simply use FCM without importing your current project into a Firebase project.
Upvotes: 1
Reputation: 5841
Actually it's not very difficult to migrate from GCM to FCM. From this, it's said that you just need to replace :
gcm-http.googleapis.com/gcm/
to
fcm.googleapis.com/fcm/
This is a sample for sending FCM with Registration Id from mysql database : first create a function to send notification using you serverKey
function send_notification($tokens, $message){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key =
AIzaYOUR_SERVER_KEY',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === FALSE){
die('Curl failed : ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
To use above function you need to query the list of registrationId, for example :
$conn = mysqli_connect("localhost","dbuser","dbpass","dbname");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("messageText" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens,$message);
echo $message_status;
Upvotes: 0
Reputation: 734
You just need to add your app to the Firebase Console. Then download the google-services.json
file and copy it in your project module(usually named as "app").
And update your project and module gradle build files also.
And also add FirebaseInstanceId & FirebaseMessaging Service to your Android Manifest file.
The FirebaseInstanceId Service automatically manages the creation and updating of the token of the particular device. From the method onTokenRefresh
you can send the token to your Mysql Database.
For demo project on Android,just go to : https://github.com/firebase/quickstart-android/tree/master/messaging
For more information on Firebase : https://firebase.google.com/docs/cloud-messaging/android/client
Upvotes: 1