Reputation: 838
I am using the code below to generate FCM Registration token for implementing device-to-device push notification service.
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
saveRegistrationToLocalStorage(refreshedToken);
}
The code generates two different kinds of token. One with 152 characters and the other with 140 characters (Have tried more than 40-50 times, the generated token has either 140 or 152 characters). The tokens are used for device-to-device push notification using POST method.
Token with 140 characters - works with firebase console, but not from device's POST method to catch a push notification.
Token with 152 characters - works fine both with firebase console and device's POST method to catch a push notification.
The questions are:
You can have a look at the POST method I am using:
private class sendPushNotification extends AsyncTask<PushNotificationParams, Void, Void> {
@Override
protected Void doInBackground(PushNotificationParams... params) {
String device_token = params[0].device_token;
String notification_body = params[0].notification_body;
String notification_title = params[0].notification_title;
String authKey = "AIzaSy.......Xz4"; // FCM AUTH KEY
String FCMurl = "https://fcm.googleapis.com/fcm/send";
try{
URL url = new URL(FCMurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
//custom data
Map<String, String> data = new HashMap<String, String>();
data.put("data_1",data_1);
data.put("data_2", data_2);
data.put("data_3", data_3);
data.put("title", notifcation_title); // Notification title
data.put("body", notification_body); // Notification body
JSONObject map_data = new JSONObject(data);
//custom data ends here
JSONObject json = new JSONObject();
json.put("to", device_token.trim());
JSONObject info = new JSONObject();
json.put("data", map_data);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
} catch (Exception e){
Log.e("Response","from http push", e);
}
return null;
}
}
Upvotes: 0
Views: 3561
Reputation: 838
As per everyone's answer, I assumed that there might be some other service in my app that's causing the truncated token. The app was previously configured for Parse Push Notification. I removed everything that were associated with PPN, and seems like the problem is gone!
Upvotes: 2
Reputation: 3800
I'm sorry, but I can't replicate the issue on my end as well. Also, it's not advisable to store the Authorization key in the client app per Google's documentation about the best practices for securely using API keys.
I'd suggest you to use an app server for device to device messaging, see the Firebase documentation for sending upstream messages on Android.
Upvotes: 0
Reputation: 38289
This is an observation and question that is too long to post as a comment.
I tested on a single device and triggered fetch of a new token by clearing app data or calling FirebaseInstanceId.getInstance().deleteInstanceId()
. The received token was always 152 characters. I observed that the first 12 characters of the token are the 11 character instance ID returned by FirebaseInstanceId.getInstance().getId()
followed by a ':' followed by a 140 character string. For example:
Instance ID = dL1lPMbSutI (11 chars)
Token = dL1lPMbSutI:APA91xLz52hcPbGg...hhHrh4h_xFr0318k (152 chars total)
When you get a 140 character token, does it have the 12 character prefix of Instance ID and colon?
Upvotes: 0