问题
I think I have completed every step necessary to convert my messaging app from GCM to FCM but it is not working. FCM is currently returing MismatchSenderId.
I have the following services defined in my AndroidManifest.xml .
<service
android:name=".MyFcmListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Right now the following listener code never gets executed, of course, because the message is getting rejected
public class MyFcmListenerService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message)
{
String from = message.getFrom();
Map data = message.getData();
String debug = "what is in data?";
//todo Process incoming message here
}
The code for the instance Id service is as follows . . .
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService
{
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String regToken = FirebaseInstanceId.getInstance().getToken();
//store token in shared memory
SPHelper.setTempString(GlobalStuff.GCT, "fcmtoken", regToken);
}
}
The sender id on my firebase console matches the project number in my google-services.json. They are both the same 12 digit number. I defined two variants to the console: com.deanblakely.myapp and com.deanblakely.myapp.debug and those two variants are also included in my json file.
BTW after all work was done I again downloaded google-services.json to make sure it was identical to the one I had. It was not. The most recent json had this one additional line but it had no effect on my issue.
"storage_bucket": "optimal-xxxxxx-824.appspot.com"
The following C# code runs on my server and does the Post to FCM. It is my same GCM posting code except for the FCM URL. (“regid” is actually the FCM token of the target phone).
private string FCMPostToPhone(string regid, string apiKey, string postData)
{
//This posts the message to Firebase Cloud Messaging (FCM)
string PostString = "{ \"registration_ids\": [ \"" + regid + "\" ], \"data\":" + postData + "}";
byte[] byteArray = Encoding.UTF8.GetBytes(PostString);
Logger.log("Post to FCM = " + PostString);
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
Request.Method = "POST";
Request.KeepAlive = false;
Request.ContentType = "application/json";
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.ContentLength = byteArray.Length;
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
// SEND MESSAGE
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
Logger.log("Unauthorized - need new token");
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
Logger.log("Response from web service isn't OK");
}
//HttpStatusCode.
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();
Logger.log("Post Response from GCM = " + responseLine);
The following is the actual logs made from the above code showing the MimatchSenderId error from FCM (although it says GCM). (the message looks weird because it’s all encrypted).
6/25/2018 12:08:43 PMDBAWP7 Post to GCM = { "registration_ids": [ "eBGLX0_IzPI:APA91bGKIRz6Qu-W-OrFjB2vY5_yS8EanepiZjkz83borQw2bvHwWLvuzNSQAr3WStWDY2ijALLBAnGgq64jWfW2YHMkAImgEKdAWZvgj7m1MXgpB9_2zH7uRdLUQNAG2jmEK3YzHYxgJBQTq_Cgz1mOpNxbDeLU3A" ], "data":{"Message":"9FnAod3oemeAH0iw8oojG5WNnmcqmiGLjIjESex0mHY\u003d\n","aeskey":"tSFErur1I8O1ADjhP5UTNdpryNwVw156bTr9Hz70//jpJoUIcEE41KOjkrt8yKpQQpSgqgj2A2/GMDXOzIN2wEVnKRqJir7Ylp85DHuj2Z0swOnEuYPUTHXv3z6iQXZlVrw65EP6TrpqyAnCppejqxuDAC2wtQI1zpfRWIlBs9sSnsucOxtpBEbIAo2AdWaBAnL9JtzuMc/xG/IzLKFsH5+DBOwQccWM4MfK8RbpVmS0RUxzSBbkUi2RBVW86ZP6vzAdliR9qCRVzr2K/x7/7IvCdmpzT5V2LM0xBiXA0Vr8BBBOygXVxM4HYplNS8C7C6eSeb/JzoGD8qj/hMvDTw\u003d\u003d","fromLang":"English","fromPhoneKey":"gbsamsung","fromUsername":"","mine":"y","subActive":"n","timeSentGMT":"2018-06-25 19:08:43","toPhoneKey":"emulator27","tran":"##03"}}
6/25/2018 12:08:44 PMDBAWP7 Post Response from GCM = {"multicast_id":5172924974567668915,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Nowhere in my project do I do anything with SENDER_ID in my code.
回答1:
I just had a similar issue. What I found was that there were more than one google-services.xml files (caused by merging), even with different names (like google-services_LOCAL_46008) that were probably included in the build, causing a wrong ID. Check your folder that contains the google-services.xml that there is only one file. One I removed any extraneous xml files, it worked.
来源:https://stackoverflow.com/questions/51031948/fcm-returning-mismatchsenderid