|
23 | 23 |
|
24 | 24 | package de.appplant.cordova.plugin.notification; |
25 | 25 |
|
26 | | -import android.annotation.SuppressLint; |
27 | 26 | import android.app.AlarmManager; |
28 | 27 | import android.app.NotificationChannel; |
29 | 28 | import android.app.NotificationManager; |
30 | 29 | import android.content.Context; |
31 | 30 | import android.content.SharedPreferences; |
| 31 | +import android.media.AudioAttributes; |
| 32 | +import android.media.RingtoneManager; |
| 33 | +import android.net.Uri; |
32 | 34 | import android.os.Build; |
33 | 35 | import android.service.notification.StatusBarNotification; |
| 36 | + |
34 | 37 | import androidx.core.app.NotificationManagerCompat; |
35 | 38 |
|
36 | 39 | import org.json.JSONException; |
|
41 | 44 | import java.util.Set; |
42 | 45 |
|
43 | 46 | import de.appplant.cordova.plugin.badge.BadgeImpl; |
| 47 | +import de.appplant.cordova.plugin.notification.util.AssetUtil; |
44 | 48 |
|
45 | 49 | import static android.os.Build.VERSION.SDK_INT; |
46 | 50 | import static android.os.Build.VERSION_CODES.M; |
47 | | -import static android.os.Build.VERSION_CODES.O; |
48 | | -import static androidx.core.app.NotificationManagerCompat.IMPORTANCE_DEFAULT; |
49 | 51 | import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID; |
50 | 52 | import static de.appplant.cordova.plugin.notification.Notification.Type.SCHEDULED; |
51 | 53 | import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERED; |
@@ -73,7 +75,6 @@ public final class Manager { |
73 | 75 | */ |
74 | 76 | private Manager(Context context) { |
75 | 77 | this.context = context; |
76 | | - createDefaultChannel(); |
77 | 78 | } |
78 | 79 |
|
79 | 80 | /** |
@@ -109,32 +110,44 @@ public boolean canScheduleExactAlarms() { |
109 | 110 | */ |
110 | 111 | public Notification schedule (Request request, Class<?> receiver) { |
111 | 112 | Options options = request.getOptions(); |
| 113 | + createChannel(options); |
112 | 114 | Notification toast = new Notification(context, options); |
113 | 115 |
|
114 | 116 | toast.schedule(request, receiver); |
115 | 117 |
|
116 | 118 | return toast; |
117 | 119 | } |
118 | 120 |
|
119 | | - /** |
120 | | - * TODO: temporary |
121 | | - */ |
122 | | - @SuppressLint("WrongConstant") |
123 | | - private void createDefaultChannel() { |
124 | | - NotificationManager mgr = getNotMgr(); |
| 121 | + private void createChannel(Options options) { |
| 122 | + String channelId = options.getChannel(); |
| 123 | + NotificationManager notificationManager = getNotMgr(); |
| 124 | + NotificationChannel channel = notificationManager.getNotificationChannel(channelId); |
| 125 | + if (channel != null) return; |
125 | 126 |
|
126 | | - if (SDK_INT < O) |
127 | | - return; |
| 127 | + channel = new NotificationChannel(channelId, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); |
128 | 128 |
|
129 | | - NotificationChannel channel = mgr.getNotificationChannel(CHANNEL_ID); |
| 129 | + Uri soundUri = getSoundUri(options.getSound()); |
| 130 | + channel.setSound( |
| 131 | + soundUri, |
| 132 | + new AudioAttributes.Builder() |
| 133 | + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 134 | + .setUsage(AudioAttributes.USAGE_NOTIFICATION) |
| 135 | + .build() |
| 136 | + ); |
130 | 137 |
|
131 | | - if (channel != null) |
132 | | - return; |
| 138 | + notificationManager.createNotificationChannel(channel); |
| 139 | + } |
133 | 140 |
|
134 | | - channel = new NotificationChannel( |
135 | | - CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT); |
| 141 | + private Uri getSoundUri(String soundPath) { |
| 142 | + if (soundPath == null || soundPath.isEmpty()) { |
| 143 | + return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 144 | + } |
136 | 145 |
|
137 | | - mgr.createNotificationChannel(channel); |
| 146 | + Uri soundUri = AssetUtil.getInstance(context).parse(soundPath); |
| 147 | + if (soundUri == null || soundUri.equals(Uri.EMPTY)) { |
| 148 | + soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 149 | + } |
| 150 | + return soundUri; |
138 | 151 | } |
139 | 152 |
|
140 | 153 | /** |
|
0 commit comments