-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LocalNotifications): add createChannel, deleteChannel and listCh…
…annels methods (#2676)
- Loading branch information
Showing
9 changed files
with
215 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...acitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.getcapacitor.plugin.notification; | ||
|
||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.graphics.Color; | ||
import android.media.AudioAttributes; | ||
import android.net.Uri; | ||
import android.util.Log; | ||
|
||
|
||
import androidx.core.app.NotificationCompat; | ||
|
||
import com.getcapacitor.JSArray; | ||
import com.getcapacitor.JSObject; | ||
import com.getcapacitor.PluginCall; | ||
|
||
import java.util.List; | ||
|
||
public class NotificationChannelManager { | ||
|
||
private Context context; | ||
private NotificationManager notificationManager; | ||
|
||
public NotificationChannelManager(Context context) { | ||
this.context = context; | ||
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
} | ||
|
||
public NotificationChannelManager(Context context, NotificationManager manager) { | ||
this.context = context; | ||
this.notificationManager = manager; | ||
} | ||
|
||
private static final String TAG = "NotificationChannel: "; | ||
|
||
|
||
private static String CHANNEL_ID = "id"; | ||
private static String CHANNEL_NAME = "name"; | ||
private static String CHANNEL_DESCRIPTION = "description"; | ||
private static String CHANNEL_IMPORTANCE = "importance"; | ||
private static String CHANNEL_VISIBILITY = "visibility"; | ||
private static String CHANNEL_SOUND = "sound"; | ||
private static String CHANNEL_USE_LIGHTS = "lights"; | ||
private static String CHANNEL_LIGHT_COLOR = "lightColor"; | ||
|
||
public void createChannel(PluginCall call) { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
JSObject channel = new JSObject(); | ||
channel.put(CHANNEL_ID, call.getString(CHANNEL_ID)); | ||
channel.put(CHANNEL_NAME, call.getString(CHANNEL_NAME)); | ||
channel.put(CHANNEL_DESCRIPTION, call.getString(CHANNEL_DESCRIPTION, "")); | ||
channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC)); | ||
channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE)); | ||
channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null)); | ||
channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false)); | ||
channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null)); | ||
createChannel(channel); | ||
call.success(); | ||
} else { | ||
call.unavailable(); | ||
} | ||
} | ||
public void createChannel(JSObject channel) { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
NotificationChannel notificationChannel = new NotificationChannel(channel.getString(CHANNEL_ID), channel.getString(CHANNEL_NAME), channel.getInteger(CHANNEL_IMPORTANCE)); | ||
notificationChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION)); | ||
notificationChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY)); | ||
notificationChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS)); | ||
String lightColor = channel.getString(CHANNEL_LIGHT_COLOR); | ||
if (lightColor != null) { | ||
try { | ||
notificationChannel.setLightColor(Color.parseColor(lightColor)); | ||
} catch (IllegalArgumentException ex) { | ||
Log.e(TAG, "Invalid color provided for light color."); | ||
} | ||
} | ||
String sound = channel.getString(CHANNEL_SOUND, null); | ||
if (sound != null && !sound.isEmpty()) { | ||
if (sound.contains(".")) { | ||
sound = sound.substring(0, sound.lastIndexOf('.')); | ||
} | ||
AudioAttributes audioAttributes = new AudioAttributes.Builder() | ||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) | ||
.setUsage(AudioAttributes.USAGE_ALARM).build(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound); | ||
notificationChannel.setSound(soundUri, audioAttributes); | ||
} | ||
notificationManager.createNotificationChannel(notificationChannel); | ||
} | ||
} | ||
|
||
public void deleteChannel(PluginCall call) { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
String channelId = call.getString("id"); | ||
notificationManager.deleteNotificationChannel(channelId); | ||
call.success(); | ||
} else { | ||
call.unavailable(); | ||
} | ||
} | ||
|
||
public void listChannels(PluginCall call) { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels(); | ||
JSArray channels = new JSArray(); | ||
for (NotificationChannel notificationChannel : notificationChannels) { | ||
JSObject channel = new JSObject(); | ||
channel.put(CHANNEL_ID, notificationChannel.getId()); | ||
channel.put(CHANNEL_NAME, notificationChannel.getName()); | ||
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription()); | ||
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance()); | ||
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility()); | ||
channel.put(CHANNEL_SOUND, notificationChannel.getSound()); | ||
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights()); | ||
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor()))); | ||
Log.d(TAG, "visibility " + notificationChannel.getLockscreenVisibility()); | ||
Log.d(TAG, "importance " + notificationChannel.getImportance()); | ||
channels.put(channel); | ||
} | ||
JSObject result = new JSObject(); | ||
result.put("channels", channels); | ||
call.success(result); | ||
} else { | ||
call.unavailable(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
1 comment
on commit d72e25d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as the above comment
@dwlrathod , I think it is better to replace AudioAttributes.USAGE_ALARM with either AudioAttributes.USAGE_NOTIFICATION or AudioAttributes.USAGE_NOTIFICATION_RINGTONE, because what I noticed that even if my phone is in vibrate or silent mode still it is playing sound and that is because of USAGE_ALARM.
@jcesarmobile , please let me know for any further clarification or if I can raise a PR for the same.
@dwlrathod , I think it is better to replace AudioAttributes.USAGE_ALARM with either AudioAttributes.USAGE_NOTIFICATION or AudioAttributes.USAGE_NOTIFICATION_RINGTONE, because what I noticed that even if my phone is in vibrate or silent mode still it is playing sound and that is because of USAGE_ALARM.
@jcesarmobile , please let me know for any further clarification or if I can raise a PR for the same.