Firebase Notification (http v1 API) shows error when I set Minify Enable = true, Access token failed: com.google.auth.oauth2

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

New contributor

Rup Sarma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật