I am using Firebase Notification for my car Manager Application for android. all code works fine if i set isMinifyEnabled = false but when set isMinifyEnabled = true like this
buildTypes {
relese {
isShrinkResources = true
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
then Notification doesn’t show.
my proguard rules are
-keep class com.gixcar.manager.model.UserModel { *; }
-keep class com.gixcar.manager.model.NotificationModel { *; }
-keep class com.gixcar.manager.utility.NotificationUtils { *; }
-keep class com.google.firebase.** { *; }
-keep class com.google.firebase.messaging.** { *; }
-keep class com.google.firebase.iid.** { *; }
-keep class com.google.firebase.** { *; }
-dontwarn com.google.firebase.**
-dontwarn com.google.android.gms.**
-keepattributes *Annotation*
Manifiest.xml
<Application>
<service
android:name=".service.NotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</Application>
Here is show notification method using Okhttp library
private static String getAccessToken() {
String AUTH_TOKEN = "{n" +
" "type": "service_account",n" +
" "project_id": "gix-car",n" +
" "private_key_id": "73263fef7e98e3e52a3aae2a84ec101a8d46dc17",n" +
" "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0jhgyutuygbhjgyikuhkjhuuinjyiuhjhi8ulkjugh40QjfcI3UVcMSlHO\nHUV0Cv/zx3YGi5/Pc+SAoskrU8CzQbC6+PGVKOQrK3ZkC++/C4w1ZQN1Wz1XlVzG\nVoieKhKXFkje1oKILWhiD3gUmEq/u+rdRILQYJEomR0ioC+89M6QOYuiIhuPwwgW\n3zm3qJZTAgMBAAx4\nZt1KRmGxUK4RPMCzL6hjLhsypA==\n-----END PRIVATE KEY-----\n",n" +
" "client_email": "firebase-adminsdk@example_email",n" +
" "client_id": "102543545454545345",n" +
" "auth_uri": "https://accounts.google.com/o/oauth2/auth",n" +
" "token_uri": "https://oauth2.googleapis.com/token",n" +
" "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",n" +
" "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-example_gserviceaccount.com",n" +
" "universe_domain": "googleapis.com"n" +
"}n";
try {
InputStream stream = new ByteArrayInputStream(AUTH_TOKEN.getBytes());
String firebaseScope = "https://www.googleapis.com/auth/firebase.messaging";
GoogleCredentials credentials = GoogleCredentials.fromStream(stream).createScoped(Lists.newArrayList(firebaseScope));
credentials.refresh();
return credentials.getAccessToken().getTokenValue();
} catch (IOException e) {
Log.e("FCM AccessToken", "Access token failed: " + e);
return null;
}
}
public static void sendNotification(String mob_no, String title, String msg, String data, Uri imageUrl) {
FirebaseUtil.userInfo(mob_no).get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
String token = task.getResult().getString("userToken");
try {
JSONObject combineObject = new JSONObject();
JSONObject notification = new JSONObject();
JSONObject messageObj = new JSONObject();
JSONObject dataObj = new JSONObject();
notification.put("title", title);
notification.put("body", msg);
notification.put("image", imageUrl);
dataObj.put("locationData", data);
messageObj.put("token", token);
messageObj.put("notification", notification);
messageObj.put("data", dataObj);
combineObject.put("message", messageObj);
callApi(combineObject);
} catch (Exception e) {
Log.e("notification error", "sendNotification: " + e.getLocalizedMessage());
}
}
});
}
private static void callApi(JSONObject jsonObject) {
MediaType jsonPayload = MediaType.get("application/json");
OkHttpClient client = new OkHttpClient();
String url = "https://fcm.googleapis.com/v1/projects/gix-car/messages:send";
RequestBody body = RequestBody.create(jsonObject.toString(), jsonPayload);
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", "Bearer " + getAccessToken())
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.e("FCM Error", "Notification failed: " + e);
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
Log.e("FCM Success", "Notification sent successfully: " + response.body().string());
} else
Log.e("FCM Error", "Notification failed: " + response.body().string());
}
});
}
I have already tried by Adding Proguard rules but not working
showing this error Access token failed: com.google.auth.oauth2. GoogleAuthException: Error getting access token for service account: 400 Bad Request POST https://oauth2.googleapis. com/ token
Rup Sarma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.