-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added channel creation utility and notification sound support
- Loading branch information
Showing
4 changed files
with
129 additions
and
25 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
88 changes: 88 additions & 0 deletions
88
...d/src/main/java/com/okode/richlocalnotifications/RichLocalNotificationChannelBuilder.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,88 @@ | ||
package com.okode.richlocalnotifications; | ||
|
||
import android.app.NotificationChannel; | ||
import android.content.Context; | ||
import android.media.AudioAttributes; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.O) | ||
public class RichLocalNotificationChannelBuilder { | ||
|
||
private String id; | ||
private String name; | ||
private String description; | ||
private int importance; | ||
private boolean showBadge; | ||
private boolean enableVibration; | ||
private int lockScreenVisibility; | ||
private String sound; | ||
|
||
public RichLocalNotificationChannelBuilder(String id, String name, int importance) { | ||
this.setId(id); | ||
this.setName(name); | ||
this.setImportance(importance); | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setId(String id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setName(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setDescription(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setImportance(int importance) { | ||
this.importance = importance; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setShowBadge(boolean showBadge) { | ||
this.showBadge = showBadge; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setEnableVibration(boolean enableVibration) { | ||
this.enableVibration = enableVibration; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setLockScreenVisibility(int lockScreenVisibility) { | ||
this.lockScreenVisibility = lockScreenVisibility; | ||
return this; | ||
} | ||
|
||
public RichLocalNotificationChannelBuilder setSound(String sound) { | ||
this.sound = sound; | ||
return this; | ||
} | ||
|
||
public NotificationChannel build(Context context) { | ||
NotificationChannel channel = new NotificationChannel(this.id, this.name, this.importance); | ||
|
||
channel.setDescription(this.description); | ||
channel.setShowBadge(this.showBadge); | ||
channel.enableVibration(this.enableVibration); | ||
channel.setLockscreenVisibility(this.lockScreenVisibility); | ||
|
||
// Audio configuration | ||
Uri soundUri = RichLocalNotificationUtils.getSound(context, this.sound); | ||
|
||
AudioAttributes audioAttributes = new AudioAttributes.Builder() | ||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) | ||
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(); | ||
|
||
channel.setSound(soundUri, audioAttributes); | ||
|
||
return channel; | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
android/src/main/java/com/okode/richlocalnotifications/RichLocalNotificationUtils.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,24 @@ | ||
package com.okode.richlocalnotifications; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.net.Uri; | ||
|
||
public class RichLocalNotificationUtils { | ||
|
||
public static final String SOUND_SYSTEM_RINGTONE = "system_ringtone"; | ||
|
||
private RichLocalNotificationUtils() { } | ||
|
||
public static Uri getSound(Context context, String sound) { | ||
Uri soundUri = android.provider.Settings.System.DEFAULT_NOTIFICATION_URI; | ||
|
||
if (SOUND_SYSTEM_RINGTONE.equalsIgnoreCase(sound)) { | ||
soundUri = android.provider.Settings.System.DEFAULT_RINGTONE_URI; | ||
} else if (sound != null && !sound.isEmpty()) { | ||
soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound); | ||
} | ||
|
||
return soundUri; | ||
} | ||
} |