Skip to content

Commit

Permalink
fix(push-notifications): fixing Android deprecations (#1467)
Browse files Browse the repository at this point in the history
  • Loading branch information
theproducer committed Mar 9, 2023
1 parent d92e163 commit 6558c5e
Showing 1 changed file with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import com.getcapacitor.annotation.Permission;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -56,10 +54,9 @@ protected void handleOnNewIntent(Intent data) {
JSObject dataObject = new JSObject();
for (String key : bundle.keySet()) {
if (key.equals("google.message_id")) {
notificationJson.put("id", bundle.get(key));
notificationJson.put("id", bundle.getString(key));
} else {
Object value = bundle.get(key);
String valueStr = (value != null) ? value.toString() : null;
String valueStr = bundle.getString(key);
dataObject.put(key, valueStr);
}
}
Expand Down Expand Up @@ -111,7 +108,7 @@ public void getDeliveredNotifications(PluginCall call) {
JSObject extras = new JSObject();

for (String key : notification.extras.keySet()) {
extras.put(key, notification.extras.get(key));
extras.put(key, notification.extras.getString(key));
}

jsNotif.put("data", extras);
Expand Down Expand Up @@ -221,14 +218,22 @@ public void fireNotification(RemoteMessage remoteMessage) {
if (presentation != null) {
if (Arrays.asList(presentation).contains("alert")) {
Bundle bundle = null;
try {
ApplicationInfo applicationInfo = getContext()
.getPackageManager()
.getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
bundle = applicationInfo.metaData;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
try {
ApplicationInfo applicationInfo = getContext()
.getPackageManager()
.getApplicationInfo(
getContext().getPackageName(),
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA)
);
bundle = applicationInfo.metaData;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
} else {
bundle = getBundleLegacy();
}

int pushIcon = android.R.drawable.ic_dialog_info;

if (bundle != null && bundle.getInt("com.google.firebase.messaging.default_notification_icon") != 0) {
Expand Down Expand Up @@ -268,4 +273,17 @@ public static PushNotificationsPlugin getPushNotificationsInstance() {
}
return null;
}

@SuppressWarnings("deprecation")
private Bundle getBundleLegacy() {
try {
ApplicationInfo applicationInfo = getContext()
.getPackageManager()
.getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
return applicationInfo.metaData;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
}

0 comments on commit 6558c5e

Please sign in to comment.