下面列出了android.app.Notification#FLAG_SHOW_LIGHTS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setNotificationLight(Notification notification) {
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
switch (NoticePreferences.getInstance().getLightColor()) {
case 0:
notification.ledARGB = Color.GREEN;
break;
case 1:
notification.ledARGB = Color.RED;
break;
case 2:
notification.ledARGB = Color.YELLOW;
break;
case 3:
notification.ledARGB = Color.BLUE;
break;
case 4:
break;
}
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
}
public static Notification build(Context context,
NotificationCompat.Builder mBuilder, Intent resultIntent) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
return notification;
}
/**
* Set the argb value that you would like the LED on the device to blnk, as well as the
* rate. The rate is specified in terms of the number of milliseconds to be on
* and then the number of milliseconds to be off.
*/
public Builder setLights(int argb, int onMs, int offMs) {
mNotification.ledARGB = argb;
mNotification.ledOnMS = onMs;
mNotification.ledOffMS = offMs;
boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0;
mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) |
(showLights ? Notification.FLAG_SHOW_LIGHTS : 0);
return this;
}
/**
* Set the argb value that you would like the LED on the device to blnk, as well as the
* rate. The rate is specified in terms of the number of milliseconds to be on
* and then the number of milliseconds to be off.
*/
public Builder setLights(int argb, int onMs, int offMs) {
mNotification.ledARGB = argb;
mNotification.ledOnMS = onMs;
mNotification.ledOffMS = offMs;
boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0;
mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) |
(showLights ? Notification.FLAG_SHOW_LIGHTS : 0);
return this;
}
private void alarmNotification(String messageToShow) {
notification.tickerText = "";
notification.vibrate = new long[]{0, 0, 0};
notification.sound = null;
if (G.isAppInFg) {
if (!isChatRoomNow) {
if (settingValue.inAppVibration) {
notification.vibrate = setVibrator(vibrator);
}
if (settingValue.inAppSound) {
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
}
if (settingValue.inAppPreview) {
notification.tickerText = messageList.get(0).name + " " + messageToShow;
}
} else if (settingValue.soundInChat) {
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
}
} else {
notification.vibrate = setVibrator(vibrator);
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
if (messagePreview) {
notification.tickerText = messageList.get(0).name + " " + messageToShow;
}
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = led;
notification.ledOnMS = 1000;
notification.ledOffMS = 2000;
currentAlarm = System.currentTimeMillis();
}
private void alarmNotification(String messageToShow) {
if (G.isAppInFg) {
if (!isChatRoomNow) {
if (inAppVibrator == 1) {
notification.vibrate = setVibrator(vibrator);
}
if (inAppSound == 1) {
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
}
if (inAppPreview == 1) {
notification.tickerText = list.get(0).name + " " + messageToShow;
}
} else if (inChat_Sound == 1) {
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
}
} else {
notification.vibrate = setVibrator(vibrator);
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + setSound(sound));
if (messagePeriview == 1) {
notification.tickerText = list.get(0).name + " " + messageToShow;
} else {
notification.tickerText = "";
}
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = led;
notification.ledOnMS = 1000;
notification.ledOffMS = 2000;
currentAlarm = System.currentTimeMillis();
}
public static void lightLed(Context context, int colorOx, int startOffMS, int durationMS) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.ledARGB = colorOx;
notification.ledOffMS = startOffMS;
notification.ledOnMS = durationMS;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
LedID++;
nm.notify(LedID, notification);
nm.cancel(LedID);
}
public static void lightLed(Context context, int colorOx, int startOffMS, int durationMS) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.ledARGB = colorOx;
notification.ledOffMS = startOffMS;
notification.ledOnMS = durationMS;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
LedID++;
nm.notify(LedID, notification);
nm.cancel(LedID);
}
/**
* 发送新消息通知栏提醒
*
* @param list 新消息
*/
public void sendMessageNotifivation(List<EMMessage> list)
{
PackageManager packageManager = FCApplication.getInstance().getPackageManager();
//将应用名设置为通知栏标题
String title = (String) packageManager
.getApplicationLabel(FCApplication.getInstance().getApplicationInfo());
String message = FCApplication.getInstance().getResources().getString(R.string.notification_message);
//创建通知栏点击意图
Intent msgIntent = new Intent(FCApplication.getInstance(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(FCApplication.getInstance(),
mBackNotifyId, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 创建notification对象
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(FCApplication.getInstance())
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_logo)//TODO 改为只使用alpha图层的Icon
.setLargeIcon(BitmapFactory.decodeResource(FCApplication.getInstance().getResources(), R.mipmap.ic_logo))
.setLights(Color.BLUE, 2000, 2000)//三色灯提醒,其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_SHOW_LIGHTS;//要支持三色灯,这个flag绝对不能少
//发送通知
mNotifyMgr.notify(mBackNotifyId, notification);
//铃声、震动
ringtongAndVibratorNotify();
}
public static Notification buildCallNotification(Context context, String roomName, String roomId, String matrixId, String callId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle(roomName);
builder.setContentText(context.getString(R.string.call_in_progress));
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.ic_menu_small_matrix);
} else {
builder.setSmallIcon(R.drawable.ic_menu_small_matrix_transparent);
}
// Build the pending intent for when the notification is clicked
Intent roomIntent = new Intent(context, RoomActivity.class);
roomIntent.putExtra(RoomActivity.EXTRA_ROOM_ID, roomId);
roomIntent.putExtra(RoomActivity.EXTRA_MATRIX_ID, matrixId);
roomIntent.putExtra(RoomActivity.EXTRA_START_CALL_ID, callId);
// Recreate the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context)
.addParentStack(RoomActivity.class)
.addNextIntent(roomIntent);
// android 4.3 issue
// use a generator for the private requestCode.
// When using 0, the intent is not created/launched when the user taps on the notification.
//
PendingIntent pendingIntent = stackBuilder.getPendingIntent((new Random()).nextInt(1000), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification n = builder.build();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.defaults |= Notification.DEFAULT_LIGHTS;
return n;
}
/**
* Set the argb value that you would like the LED on the device to blnk, as well as the
* rate. The rate is specified in terms of the number of milliseconds to be on
* and then the number of milliseconds to be off.
*/
public Builder setLights(int argb, int onMs, int offMs) {
mNotification.ledARGB = argb;
mNotification.ledOnMS = onMs;
mNotification.ledOffMS = offMs;
boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0;
mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) |
(showLights ? Notification.FLAG_SHOW_LIGHTS : 0);
return this;
}
/**
* Set the argb value that you would like the LED on the device to blnk, as well as the
* rate. The rate is specified in terms of the number of milliseconds to be on
* and then the number of milliseconds to be off.
*/
public Builder setLights(int argb, int onMs, int offMs) {
mNotification.ledARGB = argb;
mNotification.ledOnMS = onMs;
mNotification.ledOffMS = offMs;
boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0;
mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) |
(showLights ? Notification.FLAG_SHOW_LIGHTS : 0);
return this;
}
public static void nmNotifyOfWallet(NotificationManager nm, Context context,
int notifyId, Intent intent, String title,
String contentText,
int iconId, int rawId) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setSmallIcon(iconId);
builder.setContentText(contentText);
builder.setContentTitle(title);
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
builder.setWhen(System.currentTimeMillis());
Notification notification = null;
if (ServiceUtil.isNoPrompt(System.currentTimeMillis())) {
notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
notification.sound = null;
} else {
builder.setSound(Uri.parse("android.resource://"
+ context.getPackageName() + "/" + rawId));
notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xFF84E4FA;
notification.ledOnMS = 3000;
notification.ledOffMS = 2000;
}
nm.notify(notifyId, notification);
}
public static void nmNotifyDefault(NotificationManager nm, Context context,
int notifyId, Intent intent, String title,
String contentText,
int iconId) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setSmallIcon(iconId);
builder.setContentText(contentText);
builder.setContentTitle(title);
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
builder.setWhen(System.currentTimeMillis());
Notification notification = null;
notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xFF84E4FA;
notification.ledOnMS = 3000;
notification.ledOffMS = 2000;
nm.notify(notifyId, notification);
}
private void startNotification(String msg, int icon, boolean messages, int amount) {
android.support.v4.app.TaskStackBuilder stackBuilder = android.support.v4.app.TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ChatScreen.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(new Intent(context, ChatScreen.class));
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setHintHideIcon(true)
.setBackground(BitmapFactory.decodeResource(context.getResources(), (R.drawable.wearable_background)));
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
.setOngoing(false)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(msg)
.setContentIntent(resultPendingIntent)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setAutoCancel(false)
.extend(wearableExtender)
.setColor(context.getResources().getColor(R.color.primary_color));
if (amount > 0) {
nBuilder.setNumber(amount);
}
Notification notif;
if(messages) {
nBuilder.setPriority(2)
.setVibrate(new long[]{1, 1, 1});
// If audio is allowed, do it on new messages!
if (sessionData.getSessionSettings().audioFeedback()) {
nBuilder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.tone));
}
notif = nBuilder.build();
notif.ledARGB = 0xFFffffff;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
//notif.ledOnMS = 300;
//notif.ledOffMS = 300;
} else {
nBuilder.setPriority(0);
notif = nBuilder.build();
}
notificationManager.notify(AndFChatApplication.NOTIFICATION_ID, notif);
}
private void showNotification(long millisUntilFinished) {
String text;
if (millisUntilFinished > 0) {
text = getString(R.string.mobile_cells_registration_pref_dlg_status_started);
String time = getString(R.string.mobile_cells_registration_pref_dlg_status_remaining_time);
long iValue = millisUntilFinished / 1000;
time = time + ": " + GlobalGUIRoutines.getDurationString((int) iValue);
text = text + "; " + time;
if (android.os.Build.VERSION.SDK_INT < 24) {
text = text + " (" + getString(R.string.ppp_app_name) + ")";
}
}
else {
text = getString(R.string.mobile_cells_registration_pref_dlg_status_stopped);
}
PPApplication.createMobileCellsRegistrationNotificationChannel(this);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, PPApplication.MOBILE_CELLS_REGISTRATION_NOTIFICATION_CHANNEL)
.setColor(ContextCompat.getColor(this, R.color.notificationDecorationColor))
.setSmallIcon(R.drawable.ic_exclamation_notify) // notification icon
.setContentTitle(getString(R.string.phone_profiles_pref_applicationEventMobileCellsRegistration_notification)) // title for notification
.setContentText(text) // message for notification
.setAutoCancel(true); // clear notification after click
if (millisUntilFinished > 0) {
Intent stopRegistrationIntent = new Intent(ACTION_MOBILE_CELLS_REGISTRATION_STOP_BUTTON);
PendingIntent stopRegistrationPendingIntent = PendingIntent.getBroadcast(context, 0, stopRegistrationIntent, 0);
mBuilder.addAction(R.drawable.ic_action_stop_white,
context.getString(R.string.phone_profiles_pref_applicationEventMobileCellsRegistration_stop),
stopRegistrationPendingIntent);
}
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
//if (android.os.Build.VERSION.SDK_INT >= 21)
//{
mBuilder.setCategory(NotificationCompat.CATEGORY_RECOMMENDATION);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
//}
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
notification.flags &= ~Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 0;
notification.ledOffMS = 0;
notification.sound = null;
notification.vibrate = null;
notification.defaults &= ~DEFAULT_SOUND;
notification.defaults &= ~DEFAULT_VIBRATE;
startForeground(PPApplication.MOBILE_CELLS_REGISTRATION_SERVICE_NOTIFICATION_ID, notification);
}
/**
* Set the default notification options that will be used.
* <p>
* The value should be one or more of the following fields combined with
* bitwise-or:
* {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
* {@link Notification#DEFAULT_LIGHTS}.
* <p>
* For all default values, use {@link Notification#DEFAULT_ALL}.
*/
public Builder setDefaults(int defaults) {
mNotification.defaults = defaults;
if ((defaults & Notification.DEFAULT_LIGHTS) != 0) {
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
return this;
}
/**
* Set the default notification options that will be used.
* <p>
* The value should be one or more of the following fields combined with
* bitwise-or:
* {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
* {@link Notification#DEFAULT_LIGHTS}.
* <p>
* For all default values, use {@link Notification#DEFAULT_ALL}.
*/
public Builder setDefaults(int defaults) {
mNotification.defaults = defaults;
if ((defaults & Notification.DEFAULT_LIGHTS) != 0) {
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
return this;
}
/**
* Set the default notification options that will be used.
* <p>
* The value should be one or more of the following fields combined with
* bitwise-or:
* {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
* {@link Notification#DEFAULT_LIGHTS}.
* <p>
* For all default values, use {@link Notification#DEFAULT_ALL}.
*/
public Builder setDefaults(int defaults) {
mNotification.defaults = defaults;
if ((defaults & Notification.DEFAULT_LIGHTS) != 0) {
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
return this;
}
/**
* Set the default notification options that will be used.
* <p>
* The value should be one or more of the following fields combined with
* bitwise-or:
* {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE},
* {@link Notification#DEFAULT_LIGHTS}.
* <p>
* For all default values, use {@link Notification#DEFAULT_ALL}.
*/
public Builder setDefaults(int defaults) {
mNotification.defaults = defaults;
if ((defaults & Notification.DEFAULT_LIGHTS) != 0) {
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
return this;
}