Skip to content

Commit 36e4101

Browse files
committed
Merge registerNotificationChannel
1 parent 3f1de8f commit 36e4101

File tree

2 files changed

+38
-52
lines changed

2 files changed

+38
-52
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.ActivityManager;
55
import android.app.AlarmManager;
66
import android.app.Notification;
7+
import android.app.NotificationChannel;
78
import android.app.NotificationManager;
89
import android.app.PendingIntent;
910
import android.content.BroadcastReceiver;
@@ -139,8 +140,6 @@ public void setNotificationCallback(NotificationCallback notificationCallback) {
139140
UnityNotificationManager.mNotificationCallback = notificationCallback;
140141
}
141142

142-
// Register a new notification channel.
143-
// This function will only be called for devices which are low than Android O.
144143
public void registerNotificationChannel(
145144
String id,
146145
String name,
@@ -152,30 +151,43 @@ public void registerNotificationChannel(
152151
boolean canShowBadge,
153152
long[] vibrationPattern,
154153
int lockscreenVisibility) {
155-
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
156-
Set<String> channelIds = new HashSet<String>(prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>()));
157-
channelIds.add(id); // TODO: what if users create the channel again with the same id?
158-
159-
// Add to notification channel ids SharedPreferences.
160-
SharedPreferences.Editor editor = prefs.edit().clear();
161-
editor.putStringSet("ChannelIDs", channelIds);
162-
editor.apply();
163-
164-
// Store the channel into a SharedPreferences.
165-
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
166-
editor = channelPrefs.edit();
167-
168-
editor.putString("title", name); // Sadly I can't change the "title" here to "name" due to backward compatibility.
169-
editor.putInt("importance", importance);
170-
editor.putString("description", description);
171-
editor.putBoolean("enableLights", enableLights);
172-
editor.putBoolean("enableVibration", enableVibration);
173-
editor.putBoolean("canBypassDnd", canBypassDnd);
174-
editor.putBoolean("canShowBadge", canShowBadge);
175-
editor.putString("vibrationPattern", Arrays.toString(vibrationPattern));
176-
editor.putInt("lockscreenVisibility", lockscreenVisibility);
177-
178-
editor.apply();
154+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
155+
NotificationChannel channel = new NotificationChannel(id, name, importance);
156+
channel.setDescription(description);
157+
channel.enableLights(enableLights);
158+
channel.enableVibration(enableVibration);
159+
channel.setBypassDnd(canBypassDnd);
160+
channel.setShowBadge(canShowBadge);
161+
channel.setVibrationPattern(vibrationPattern);
162+
channel.setLockscreenVisibility(lockscreenVisibility);
163+
164+
getNotificationManager().createNotificationChannel(channel);
165+
} else {
166+
SharedPreferences prefs = mContext.getSharedPreferences(NOTIFICATION_CHANNELS_SHARED_PREFS, Context.MODE_PRIVATE);
167+
Set<String> channelIds = new HashSet<String>(prefs.getStringSet(NOTIFICATION_CHANNELS_SHARED_PREFS_KEY, new HashSet<String>()));
168+
channelIds.add(id); // TODO: what if users create the channel again with the same id?
169+
170+
// Add to notification channel ids SharedPreferences.
171+
SharedPreferences.Editor editor = prefs.edit().clear();
172+
editor.putStringSet("ChannelIDs", channelIds);
173+
editor.apply();
174+
175+
// Store the channel into a SharedPreferences.
176+
SharedPreferences channelPrefs = mContext.getSharedPreferences(getSharedPrefsNameByChannelId(id), Context.MODE_PRIVATE);
177+
editor = channelPrefs.edit();
178+
179+
editor.putString("title", name); // Sadly I can't change the "title" here to "name" due to backward compatibility.
180+
editor.putInt("importance", importance);
181+
editor.putString("description", description);
182+
editor.putBoolean("enableLights", enableLights);
183+
editor.putBoolean("enableVibration", enableVibration);
184+
editor.putBoolean("canBypassDnd", canBypassDnd);
185+
editor.putBoolean("canShowBadge", canShowBadge);
186+
editor.putString("vibrationPattern", Arrays.toString(vibrationPattern));
187+
editor.putInt("lockscreenVisibility", lockscreenVisibility);
188+
189+
editor.apply();
190+
}
179191
}
180192

181193
protected static String getSharedPrefsNameByChannelId(String id)

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManagerOreo.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,6 @@ public UnityNotificationManagerOreo(Context context, Activity activity) {
1313
super(context, activity);
1414
}
1515

16-
@Override
17-
public void registerNotificationChannel(
18-
String id,
19-
String name,
20-
int importance,
21-
String description,
22-
boolean enableLights,
23-
boolean enableVibration,
24-
boolean canBypassDnd,
25-
boolean canShowBadge,
26-
long[] vibrationPattern,
27-
int lockscreenVisibility) {
28-
assert Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
29-
30-
NotificationChannel channel = new NotificationChannel(id, name, importance);
31-
channel.setDescription(description);
32-
channel.enableLights(enableLights);
33-
channel.enableVibration(enableVibration);
34-
channel.setBypassDnd(canBypassDnd);
35-
channel.setShowBadge(canShowBadge);
36-
channel.setVibrationPattern(vibrationPattern);
37-
channel.setLockscreenVisibility(lockscreenVisibility);
38-
39-
getNotificationManager().createNotificationChannel(channel);
40-
}
41-
4216
protected static NotificationChannelWrapper getOreoNotificationChannel(Context context, String id) {
4317
assert Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
4418

0 commit comments

Comments
 (0)