下面列出了android.app.NotificationChannel#enableLights ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.O)
private void buildNotificationChannel() {
NotificationChannel channel =
new NotificationChannel(NOTIFICATION_CHANNEL,
service.getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW);
channel.enableLights(false);
channel.enableVibration(false);
channel.setShowBadge(false);
NotificationManager notificationManager =
(NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null)
notificationManager.createNotificationChannel(channel);
}
@TargetApi(Build.VERSION_CODES.O)
public void createNotificationChannel() {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
NotificationChannel channel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_ID_RS,
getString(R.string.notification_channel_running_status),
NotificationManager.IMPORTANCE_DEFAULT
);
channel.setShowBadge(false);
channel.enableLights(false);
channel.enableVibration(false);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
}
private void initialize(){
android.app.NotificationManager notificationManager = (android.app.NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(weMessage.NOTIFICATION_CHANNEL_NAME) == null) {
NotificationChannel channel = new NotificationChannel(weMessage.NOTIFICATION_CHANNEL_NAME, app.getString(R.string.notification_channel_name), android.app.NotificationManager.IMPORTANCE_HIGH);
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build();
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.setVibrationPattern(new long[]{1000, 1000});
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
notificationManager.createNotificationChannel(channel);
}
}
@RequiresApi(Build.VERSION_CODES.O)
public static String createSilentNotificationChannel(String channelId, String channelName, Context context){
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setDescription("Silent channel");
chan.setSound(null,null);
chan.enableLights(false);
chan.setLightColor(Color.BLUE);
chan.enableVibration(false);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (service != null) {
service.createNotificationChannel(chan);
}
return channelId;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("com.xyoye.dandanplay.smbservice.playchannel", "SMB服务", NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(false);
channel.setVibrationPattern(new long[]{0});
channel.enableLights(false);
channel.setSound(null, null);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
startForeground(NOTIFICATION_ID, buildNotification());
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onPause() {
unbindService(mSC);
if (mService != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("de.vier_bier.habpanelviewer.status", "Status", NotificationManager.IMPORTANCE_MIN);
channel.enableLights(false);
channel.setSound(null, null);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "de.vier_bier.habpanelviewer.status");
builder.setSmallIcon(R.drawable.logo);
mService.startForeground(42, builder.build());
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pauseWebview = prefs.getBoolean(Constants.PREF_PAUSE_WEBVIEW, false);
if (pauseWebview && !((PowerManager) getSystemService(POWER_SERVICE)).isInteractive()) {
mWebView.pause();
}
super.onPause();
}
/**
* for API 26+ create notification channels
*/
private void createchannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel(id,
getString(R.string.channel_name), //name of the channel
NotificationManager.IMPORTANCE_DEFAULT); //importance level
//important level: default is is high on the phone. high is urgent on the phone. low is medium, so none is low?
// Configure the notification channel.
mChannel.setDescription(getString(R.string.channel_description));
mChannel.enableLights(true);
//Sets the notification light color for notifications posted to this channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setShowBadge(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
nm.createNotificationChannel(mChannel);
}
}
private void createChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "hint_service";
CharSequence channelName = "move hint";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(notificationChannel);
}
public void createNotificationChannel(){
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel mNotificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID, getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH);
mNotificationChannel.enableLights(true);
mNotificationChannel.setLightColor(Color.RED);
mNotificationChannel.enableVibration(true);
mNotificationChannel.setDescription(getString(R.string.channel_desc));
mNotificationManager.createNotificationChannel(mNotificationChannel);
}
}
public void createNotificationChannel(){
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID, getString(R.string.mascot_notification), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription(getString(R.string.notify_from_mascot));
mNotifyManager.createNotificationChannel(notificationChannel);
}
}
private void createNotificationChannel() {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = ACCOUNT_TRANSFER_CHANNEL;
CharSequence name = "AccountTransfer";
String description = "Account Transfer";
int importance = NotificationManager.IMPORTANCE_MIN;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(false);
mChannel.enableVibration(false);
mNotificationManager.createNotificationChannel(mChannel);
}
private void createNotificationChannel() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID, getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.WHITE);
notificationChannel.enableVibration(true);
notificationChannel.setDescription(getString(R.string.channel_desc));
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "应用更新", NotificationManager.IMPORTANCE_LOW);
mChannel.setDescription("应用有新版本");
mChannel.enableLights(true); //是否在桌面icon右上角展示小红点
getManager().createNotificationChannel(mChannel);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private synchronized void createAppNotificationChannel() throws ApplozicException {
CharSequence name = MobiComKitConstants.APP_NOTIFICATION_NAME;
int importance = NotificationManager.IMPORTANCE_HIGH;
if (mNotificationManager != null && mNotificationManager.getNotificationChannel(MobiComKitConstants.AL_APP_NOTIFICATION) == null) {
NotificationChannel mChannel = new NotificationChannel(MobiComKitConstants.AL_APP_NOTIFICATION, name, importance);
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
mChannel.setShowBadge(ApplozicClient.getInstance(context).isUnreadCountBadgeEnabled());
if (ApplozicClient.getInstance(context).getVibrationOnNotification()) {
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
if (TextUtils.isEmpty(soundFilePath)) {
throw new ApplozicException("Custom sound path is required to create App notification channel. " +
"Please set a sound path using Applozic.getInstance(context).setCustomNotificationSound(your-sound-file-path)");
}
mChannel.setSound(Uri.parse(soundFilePath), audioAttributes);
mNotificationManager.createNotificationChannel(mChannel);
Utils.printLog(context, TAG, "Created app notification channel");
}
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.earthquake_channel_name);
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL,
name,
NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.enableLights(true);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
static private NotificationChannel updateNotificationChannel( Context context, NotificationChannel notificationChannel, final AndroidPushTemplate template )
{
if( template.getShowBadge() != null )
notificationChannel.setShowBadge( template.getShowBadge() );
if( template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6 )
notificationChannel.setImportance( template.getPriority() ); // NotificationManager.IMPORTANCE_DEFAULT
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage( AudioAttributes.USAGE_NOTIFICATION_RINGTONE )
.setContentType( AudioAttributes.CONTENT_TYPE_SONIFICATION )
.setFlags( AudioAttributes.FLAG_AUDIBILITY_ENFORCED )
.setLegacyStreamType( AudioManager.STREAM_NOTIFICATION )
.build();
notificationChannel.setSound( PushTemplateHelper.getSoundUri( context, template.getSound() ), audioAttributes );
if (template.getLightsColor() != null)
{
notificationChannel.enableLights( true );
notificationChannel.setLightColor( template.getLightsColor()|0xFF000000 );
}
if( template.getVibrate() != null && template.getVibrate().length > 0 )
{
long[] vibrate = new long[ template.getVibrate().length ];
int index = 0;
for( long l : template.getVibrate() )
vibrate[ index++ ] = l;
notificationChannel.enableVibration( true );
notificationChannel.setVibrationPattern( vibrate );
}
return notificationChannel;
}
@Override
public void onCreate() {
super.onCreate();
try {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, CallActivity.class);
intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder =
new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(getString(R.string.app_name))
.setContentTitle(getString(R.string.TapToContiueAsVideoCallIsOn))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_CALL);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(channelId);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel =
new NotificationChannel(channelId, "test", importance);
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(false);
notificationChannel.setSound(null, null);
mNotificationManager.createNotificationChannel(notificationChannel);
}
mNotificationManager.notify(notifyId, builder.build());
startForeground(notifyId, builder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(26)
private void createNotificationChannels() {
List<NotificationChannel> notificationChannels = new ArrayList<>();
NotificationChannel recordingNotificationChannel = new NotificationChannel(
Const.RECORDING_NOTIFICATION_CHANNEL_ID,
Const.RECORDING_NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
recordingNotificationChannel.enableLights(true);
recordingNotificationChannel.setLightColor(Color.RED);
recordingNotificationChannel.setShowBadge(true);
recordingNotificationChannel.enableVibration(true);
recordingNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannels.add(recordingNotificationChannel);
NotificationChannel shareNotificationChannel = new NotificationChannel(
Const.SHARE_NOTIFICATION_CHANNEL_ID,
Const.SHARE_NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
shareNotificationChannel.enableLights(true);
shareNotificationChannel.setLightColor(Color.RED);
shareNotificationChannel.setShowBadge(true);
shareNotificationChannel.enableVibration(true);
shareNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannels.add(shareNotificationChannel);
getManager().createNotificationChannels(notificationChannels);
}
private void sendNotification(Channel channel, String notificationTitle, String notificationBody, Bitmap bitmap, Intent intent, Intent backIntent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent;
if (backIntent != null) {
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent[] intents = new Intent[]{backIntent, intent};
pendingIntent = PendingIntent.getActivities(this, notificationId++, intents, PendingIntent.FLAG_ONE_SHOT);
} else {
pendingIntent = PendingIntent.getActivity(this, notificationId++, intent, PendingIntent.FLAG_ONE_SHOT);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel.id);
notificationBuilder.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.drawable.ic_push_notification_small) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setLargeIcon(bitmap)
.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channel.id, getString(channel.name), importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ContextCompat.getColor(this, R.color.primary));
notificationChannel.enableVibration(true);
notificationBuilder.setChannelId(channel.id);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationId++, notificationBuilder.build());
}
@CordovaMethod
private void createChannel(JSONObject options, CallbackContext callbackContext) throws JSONException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
throw new UnsupportedOperationException("Notification channels are not supported");
}
String channelId = options.getString("id");
String channelName = options.getString("name");
int importance = options.getInt("importance");
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setDescription(options.optString("description", ""));
channel.setShowBadge(options.optBoolean("badge", true));
channel.enableLights(options.optBoolean("light", false));
channel.setLightColor(options.optInt("lightColor", 0));
String soundName = options.optString("sound", "default");
if (!"default".equals(soundName)) {
String packageName = cordova.getActivity().getPackageName();
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + soundName);
channel.setSound(soundUri, new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build());
}
JSONArray vibrationPattern = options.optJSONArray("vibration");
if (vibrationPattern != null) {
int patternLength = vibrationPattern.length();
long[] patternArray = new long[patternLength];
for (int i = 0; i < patternLength; i++) {
patternArray[i] = vibrationPattern.getLong(i);
}
channel.setVibrationPattern(patternArray);
channel.enableVibration(true);
} else {
channel.enableVibration(options.optBoolean("vibration", true));
}
notificationManager.createNotificationChannel(channel);
callbackContext.success();
}