|
| 1 | +package com.wix.reactnativenotifications.core.notification; |
| 2 | + |
| 3 | +import android.app.NotificationManager; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.media.RingtoneManager; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Build; |
| 9 | +import android.os.Bundle; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class NotificationChannel implements INotificationChannel { |
| 14 | + |
| 15 | + final protected Context mContext; |
| 16 | + final protected NotificationChannelProps mNotificationChannelProps; |
| 17 | + |
| 18 | + protected NotificationChannel(Context context, Bundle bundle) { |
| 19 | + mContext = context; |
| 20 | + mNotificationChannelProps = createProps(bundle); |
| 21 | + } |
| 22 | + |
| 23 | + public static INotificationChannel get(Context context, Bundle bundle) { |
| 24 | + return new NotificationChannel(context, bundle); |
| 25 | + } |
| 26 | + |
| 27 | + protected NotificationChannelProps createProps(Bundle bundle) { |
| 28 | + return new NotificationChannelProps(bundle); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void setNotificationChannel() { |
| 33 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { |
| 34 | + return; |
| 35 | + } |
| 36 | + android.app.NotificationChannel channel = new android.app.NotificationChannel( |
| 37 | + mNotificationChannelProps.getChannelId(), |
| 38 | + mNotificationChannelProps.getName(), |
| 39 | + mNotificationChannelProps.getImportance() |
| 40 | + ); |
| 41 | + |
| 42 | + if (mNotificationChannelProps.hasDescription()) { |
| 43 | + channel.setDescription(mNotificationChannelProps.getDescription()); |
| 44 | + } |
| 45 | + if (mNotificationChannelProps.hasEnableLights()) { |
| 46 | + channel.enableLights(mNotificationChannelProps.getEnableLights()); |
| 47 | + } |
| 48 | + if (mNotificationChannelProps.hasEnableVibration()) { |
| 49 | + channel.enableVibration(mNotificationChannelProps.getEnableVibration()); |
| 50 | + } |
| 51 | + if (mNotificationChannelProps.hasGroupId()) { |
| 52 | + channel.setGroup(mNotificationChannelProps.getGroupId()); |
| 53 | + } |
| 54 | + if (mNotificationChannelProps.hasLightColor()) { |
| 55 | + channel.setLightColor(Color.parseColor(mNotificationChannelProps.getLightColor())); |
| 56 | + } |
| 57 | + if (mNotificationChannelProps.hasShowBadge()) { |
| 58 | + channel.setShowBadge(mNotificationChannelProps.getShowBadge()); |
| 59 | + } |
| 60 | + if (mNotificationChannelProps.hasSoundFile()) { |
| 61 | + channel.setSound(getSound(mNotificationChannelProps.getSoundFile()), null); |
| 62 | + } |
| 63 | + if (mNotificationChannelProps.hasVibrationPattern()) { |
| 64 | + channel.setVibrationPattern( |
| 65 | + createVibrationPatternFromList(mNotificationChannelProps.getVibrationPattern()) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + final NotificationManager notificationManager = (NotificationManager) mContext |
| 70 | + .getSystemService(Context.NOTIFICATION_SERVICE); |
| 71 | + notificationManager.createNotificationChannel(channel); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public NotificationChannelProps asProps() { |
| 76 | + return mNotificationChannelProps.copy(); |
| 77 | + } |
| 78 | + |
| 79 | + private long[] createVibrationPatternFromList(List patternRequest) { |
| 80 | + if (patternRequest == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + long[] pattern = new long[patternRequest.size()]; |
| 85 | + for (int i = 0; i < patternRequest.size(); i++) { |
| 86 | + if (patternRequest.get(i) instanceof Number) { |
| 87 | + pattern[i] = ((Number) patternRequest.get(i)).longValue(); |
| 88 | + } |
| 89 | + } |
| 90 | + return pattern; |
| 91 | + } |
| 92 | + |
| 93 | + private Uri getSound(String sound) { |
| 94 | + if (sound == null) { |
| 95 | + return null; |
| 96 | + } else if (sound.contains("://")) { |
| 97 | + return Uri.parse(sound); |
| 98 | + } else if (sound.equalsIgnoreCase("default")) { |
| 99 | + return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 100 | + } else { |
| 101 | + int soundResourceId = getResourceId("raw", sound); |
| 102 | + if (soundResourceId == 0) { |
| 103 | + soundResourceId = getResourceId("raw", sound.substring(0, sound.lastIndexOf('.'))); |
| 104 | + } |
| 105 | + return Uri.parse("android.resource://" + mContext.getPackageName() + "/" + soundResourceId); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private int getResourceId(String type, String image) { |
| 110 | + return mContext |
| 111 | + .getResources() |
| 112 | + .getIdentifier(image, type, mContext.getPackageName()); |
| 113 | + } |
| 114 | +} |
0 commit comments