Skip to content

Commit e537df1

Browse files
authored
refactor(push-notifications)!: remove unneeded code (#2546)
1 parent 35a53c6 commit e537df1

4 files changed

Lines changed: 79 additions & 98 deletions

File tree

push-notifications/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Android Studio has an icon generator you can use to create your Push Notificatio
6161

6262
## Push Notification channel
6363

64-
From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. The SDK will derive the `channelId` for incoming push notifications in the following order:
64+
The SDK will derive the `channelId` for incoming push notifications in the following order:
6565

6666
1. **Firstly it will check if the incoming notification has a `channelId` set.**
6767
When sending a push notification from either the FCM dashboard, or through their API, it's possible to specify a `channelId`.
@@ -295,7 +295,7 @@ createChannel(channel: Channel) => Promise<void>
295295

296296
Create a notification channel.
297297

298-
Only available on Android O or newer (SDK 26+).
298+
Only available on Android.
299299

300300
| Param | Type |
301301
| ------------- | ------------------------------------------- |
@@ -314,7 +314,7 @@ deleteChannel(args: { id: string; }) => Promise<void>
314314

315315
Delete a notification channel.
316316

317-
Only available on Android O or newer (SDK 26+).
317+
Only available on Android.
318318

319319
| Param | Type |
320320
| ---------- | ---------------------------- |
@@ -333,7 +333,7 @@ listChannels() => Promise<ListChannelsResult>
333333

334334
List the available notification channels.
335335

336-
Only available on Android O or newer (SDK 26+).
336+
Only available on Android.
337337

338338
**Returns:** <code>Promise&lt;<a href="#listchannelsresult">ListChannelsResult</a>&gt;</code>
339339

push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/NotificationChannelManager.java

Lines changed: 71 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@
66
import android.content.Context;
77
import android.media.AudioAttributes;
88
import android.net.Uri;
9-
import android.os.Build;
10-
import android.provider.Settings;
119
import androidx.core.app.NotificationCompat;
1210
import com.getcapacitor.*;
1311
import com.getcapacitor.util.WebColor;
14-
import java.util.Arrays;
1512
import java.util.List;
1613

1714
public class NotificationChannelManager {
1815

1916
private Context context;
2017
private NotificationManager notificationManager;
21-
private PluginConfig config;
2218

23-
public NotificationChannelManager(Context context, NotificationManager manager, PluginConfig config) {
19+
public NotificationChannelManager(Context context, NotificationManager manager) {
2420
this.context = context;
2521
this.notificationManager = manager;
26-
this.config = config;
2722
}
2823

2924
private static String CHANNEL_ID = "id";
@@ -37,104 +32,90 @@ public NotificationChannelManager(Context context, NotificationManager manager,
3732
private static String CHANNEL_LIGHT_COLOR = "lightColor";
3833

3934
public void createChannel(PluginCall call) {
40-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
41-
JSObject channel = new JSObject();
42-
if (call.getString(CHANNEL_ID) != null) {
43-
channel.put(CHANNEL_ID, call.getString(CHANNEL_ID));
44-
} else {
45-
call.reject("Channel missing identifier");
46-
return;
47-
}
48-
if (call.getString(CHANNEL_NAME) != null) {
49-
channel.put(CHANNEL_NAME, call.getString(CHANNEL_NAME));
50-
} else {
51-
call.reject("Channel missing name");
52-
return;
53-
}
54-
55-
channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));
56-
channel.put(CHANNEL_DESCRIPTION, call.getString(CHANNEL_DESCRIPTION, ""));
57-
channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC));
58-
channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null));
59-
channel.put(CHANNEL_VIBRATE, call.getBoolean(CHANNEL_VIBRATE, false));
60-
channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false));
61-
channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null));
62-
createChannel(channel);
63-
call.resolve();
35+
JSObject channel = new JSObject();
36+
if (call.getString(CHANNEL_ID) != null) {
37+
channel.put(CHANNEL_ID, call.getString(CHANNEL_ID));
6438
} else {
65-
call.unavailable();
39+
call.reject("Channel missing identifier");
40+
return;
6641
}
42+
if (call.getString(CHANNEL_NAME) != null) {
43+
channel.put(CHANNEL_NAME, call.getString(CHANNEL_NAME));
44+
} else {
45+
call.reject("Channel missing name");
46+
return;
47+
}
48+
49+
channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));
50+
channel.put(CHANNEL_DESCRIPTION, call.getString(CHANNEL_DESCRIPTION, ""));
51+
channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC));
52+
channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null));
53+
channel.put(CHANNEL_VIBRATE, call.getBoolean(CHANNEL_VIBRATE, false));
54+
channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false));
55+
channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null));
56+
createChannel(channel);
57+
call.resolve();
6758
}
6859

6960
public void createChannel(JSObject channel) {
70-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
71-
NotificationChannel notificationChannel = new NotificationChannel(
72-
channel.getString(CHANNEL_ID),
73-
channel.getString(CHANNEL_NAME),
74-
channel.getInteger(CHANNEL_IMPORTANCE)
75-
);
76-
notificationChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION));
77-
notificationChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY));
78-
notificationChannel.enableVibration(channel.getBool(CHANNEL_VIBRATE));
79-
notificationChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS));
80-
String lightColor = channel.getString(CHANNEL_LIGHT_COLOR);
81-
if (lightColor != null) {
82-
try {
83-
notificationChannel.setLightColor(WebColor.parseColor(lightColor));
84-
} catch (IllegalArgumentException ex) {
85-
Logger.error(Logger.tags("NotificationChannel"), "Invalid color provided for light color.", null);
86-
}
61+
NotificationChannel notificationChannel = new NotificationChannel(
62+
channel.getString(CHANNEL_ID),
63+
channel.getString(CHANNEL_NAME),
64+
channel.getInteger(CHANNEL_IMPORTANCE)
65+
);
66+
notificationChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION));
67+
notificationChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY));
68+
notificationChannel.enableVibration(channel.getBool(CHANNEL_VIBRATE));
69+
notificationChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS));
70+
String lightColor = channel.getString(CHANNEL_LIGHT_COLOR);
71+
if (lightColor != null) {
72+
try {
73+
notificationChannel.setLightColor(WebColor.parseColor(lightColor));
74+
} catch (IllegalArgumentException ex) {
75+
Logger.error(Logger.tags("NotificationChannel"), "Invalid color provided for light color.", null);
8776
}
88-
String sound = channel.getString(CHANNEL_SOUND, null);
89-
if (sound != null && !sound.isEmpty()) {
90-
if (sound.contains(".")) {
91-
sound = sound.substring(0, sound.lastIndexOf('.'));
92-
}
93-
AudioAttributes audioAttributes = new AudioAttributes.Builder()
94-
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
95-
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
96-
.build();
97-
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound);
98-
notificationChannel.setSound(soundUri, audioAttributes);
77+
}
78+
String sound = channel.getString(CHANNEL_SOUND, null);
79+
if (sound != null && !sound.isEmpty()) {
80+
if (sound.contains(".")) {
81+
sound = sound.substring(0, sound.lastIndexOf('.'));
9982
}
100-
notificationManager.createNotificationChannel(notificationChannel);
83+
AudioAttributes audioAttributes = new AudioAttributes.Builder()
84+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
85+
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
86+
.build();
87+
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound);
88+
notificationChannel.setSound(soundUri, audioAttributes);
10189
}
90+
notificationManager.createNotificationChannel(notificationChannel);
10291
}
10392

10493
public void deleteChannel(PluginCall call) {
105-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
106-
String channelId = call.getString("id");
107-
notificationManager.deleteNotificationChannel(channelId);
108-
call.resolve();
109-
} else {
110-
call.unavailable();
111-
}
94+
String channelId = call.getString("id");
95+
notificationManager.deleteNotificationChannel(channelId);
96+
call.resolve();
11297
}
11398

11499
public void listChannels(PluginCall call) {
115-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
116-
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
117-
JSArray channels = new JSArray();
118-
for (NotificationChannel notificationChannel : notificationChannels) {
119-
JSObject channel = new JSObject();
120-
channel.put(CHANNEL_ID, notificationChannel.getId());
121-
channel.put(CHANNEL_NAME, notificationChannel.getName());
122-
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
123-
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
124-
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
125-
channel.put(CHANNEL_SOUND, notificationChannel.getSound());
126-
channel.put(CHANNEL_VIBRATE, notificationChannel.shouldVibrate());
127-
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
128-
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
129-
Logger.debug(Logger.tags("NotificationChannel"), "visibility " + notificationChannel.getLockscreenVisibility());
130-
Logger.debug(Logger.tags("NotificationChannel"), "importance " + notificationChannel.getImportance());
131-
channels.put(channel);
132-
}
133-
JSObject result = new JSObject();
134-
result.put("channels", channels);
135-
call.resolve(result);
136-
} else {
137-
call.unavailable();
100+
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
101+
JSArray channels = new JSArray();
102+
for (NotificationChannel notificationChannel : notificationChannels) {
103+
JSObject channel = new JSObject();
104+
channel.put(CHANNEL_ID, notificationChannel.getId());
105+
channel.put(CHANNEL_NAME, notificationChannel.getName());
106+
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
107+
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
108+
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
109+
channel.put(CHANNEL_SOUND, notificationChannel.getSound());
110+
channel.put(CHANNEL_VIBRATE, notificationChannel.shouldVibrate());
111+
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
112+
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
113+
Logger.debug(Logger.tags("NotificationChannel"), "visibility " + notificationChannel.getLockscreenVisibility());
114+
Logger.debug(Logger.tags("NotificationChannel"), "importance " + notificationChannel.getImportance());
115+
channels.put(channel);
138116
}
117+
JSObject result = new JSObject();
118+
result.put("channels", channels);
119+
call.resolve(result);
139120
}
140121
}

push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void load() {
5151
lastMessage = null;
5252
}
5353

54-
notificationChannelManager = new NotificationChannelManager(getActivity(), notificationManager, getConfig());
54+
notificationChannelManager = new NotificationChannelManager(getActivity(), notificationManager);
5555
}
5656

5757
@Override

push-notifications/src/definitions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export interface PushNotificationsPlugin {
7575
/**
7676
* Create a notification channel.
7777
*
78-
* Only available on Android O or newer (SDK 26+).
78+
* Only available on Android.
7979
*
8080
* @since 1.0.0
8181
*/
@@ -84,7 +84,7 @@ export interface PushNotificationsPlugin {
8484
/**
8585
* Delete a notification channel.
8686
*
87-
* Only available on Android O or newer (SDK 26+).
87+
* Only available on Android.
8888
*
8989
* @since 1.0.0
9090
*/
@@ -93,7 +93,7 @@ export interface PushNotificationsPlugin {
9393
/**
9494
* List the available notification channels.
9595
*
96-
* Only available on Android O or newer (SDK 26+).
96+
* Only available on Android.
9797
*
9898
* @since 1.0.0
9999
*/

0 commit comments

Comments
 (0)