下面列出了怎么用android.support.v4.app.NotificationCompat.Builder的API类实例代码及写法,或者点击链接到github查看源代码。
private void backupFound(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
if(jbBackupM.toString().trim().length()>0){
Intent dialogIntent = new Intent(this, AddToContactList.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
dialogIntent.putExtra("data", jbBackupM.toString());
PendingIntent intent = PendingIntent.getActivity(this, 0, dialogIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intent);
}
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("You have lost some contact, we have backup");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
private void backupFound(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
if(jbBackupM.toString().trim().length()>0){
Intent dialogIntent = new Intent(this, AddToContactList.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
dialogIntent.putExtra("data", jbBackupM.toString());
PendingIntent intent = PendingIntent.getActivity(this, 0, dialogIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intent);
}
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("You have lost some contact, we have backup");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
public NotificationBuilder setRingtone(String ringtone) {
// Ringtone options
if (ringtone != null && ringtone.length() > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
//mNotificationChannel.setSound(Uri.parse(ringtone), att);
// if not a silent sound, play
if (!ringtone.equals(mContext.getString(R.string.pref_bn_none_notification_action_ringtone))) {
Ringtone r = RingtoneManager.getRingtone(mContext.getApplicationContext(), Uri.parse(ringtone));
try {
r.play();
} catch (Exception e) {
}
}
} else {
mBuilder.setSound(Uri.parse(ringtone), AudioManager.STREAM_NOTIFICATION);
}
}
return this;
}
/**
* Starts the Notification to show the progress of video upload.
*/
private void startNotification() {
// Gets access to the Android Notification Service.
mNotifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Create the Notification and set a progress indicator for an
// operation of indeterminate length.
mBuilder = new NotificationCompat
.Builder(this)
.setContentTitle("Video Upload")
.setContentText("Upload in progress")
.setSmallIcon(android.R.drawable.stat_sys_upload)
.setTicker("Uploading video")
.setProgress(0,
0,
true);
// Build and issue the notification.
mNotifyManager.notify(NOTIFICATION_ID,
mBuilder.build());
}
/**
* Starts the Notification to show the progress of video upload.
*/
private void startNotification() {
// Gets access to the Android Notification Service.
mNotifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Create the Notification and set a progress indicator for an
// operation of indeterminate length.
mBuilder = new NotificationCompat
.Builder(this)
.setContentTitle("Video Upload")
.setContentText("Upload in progress")
.setSmallIcon(android.R.drawable.stat_sys_upload)
.setTicker("Uploading video")
.setProgress(0,
0,
true);
// Build and issue the notification.
mNotifyManager.notify(NOTIFICATION_ID,
mBuilder.build());
}
public Notification getOngoingCallNotification(final AbstractJingleConnection.Id id, final Set<Media> media) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mXmppConnectionService, "ongoing_calls");
if (media.contains(Media.VIDEO)) {
builder.setSmallIcon(R.drawable.ic_videocam_white_24dp);
builder.setContentTitle(mXmppConnectionService.getString(R.string.ongoing_video_call));
} else {
builder.setSmallIcon(R.drawable.ic_call_white_24dp);
builder.setContentTitle(mXmppConnectionService.getString(R.string.ongoing_call));
}
builder.setContentText(id.account.getRoster().getContact(id.with).getDisplayName());
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setCategory(NotificationCompat.CATEGORY_CALL);
builder.setContentIntent(createPendingRtpSession(id, Intent.ACTION_VIEW, 101));
builder.setOngoing(true);
builder.addAction(new NotificationCompat.Action.Builder(
R.drawable.ic_call_end_white_48dp,
mXmppConnectionService.getString(R.string.hang_up),
createCallAction(id.sessionId, XmppConnectionService.ACTION_END_CALL, 104))
.build());
return builder.build();
}
private Person getPerson(Message message) {
final Contact contact = message.getContact();
final Person.Builder builder = new Person.Builder();
if (contact != null) {
builder.setName(contact.getDisplayName());
final Uri uri = contact.getSystemAccount();
if (uri != null) {
builder.setUri(uri.toString());
}
} else {
builder.setName(UIHelper.getMessageDisplayName(message));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false)));
}
return builder.build();
}
void updateNotification(String contentText) {
if(mNotificationBuilder == null) {
Context context = getApplicationContext();
Intent activityIntent = new Intent(context, MobileMainActivity.class);
activityIntent.setAction(Intent.ACTION_MAIN);
activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK);
Intent stopIntent = new Intent(context, WearMessageHandlerService.class);
stopIntent.setAction(ACTION_STOP);
mNotificationBuilder = (new Builder(this))
.setSmallIcon(R.drawable.icon_notification)
.setContentTitle(getString(R.string.notification_tittle))
.setContentIntent(PendingIntent.getActivity(context, 0, activityIntent, 0))
.setLocalOnly(true)
.addAction(R.drawable.icon_power, getString(R.string.action_stop),
PendingIntent.getService(context, 0, stopIntent, 0));
}
mNotificationBuilder.setContentText(contentText);
startForeground(1, mNotificationBuilder.build());
}
public static Builder createNotificationBuilder(Context context) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setLights(context.getResources().getColor(R.color.bc_green_blue_color), 1000, 1000);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean doVibrate = sharedPrefs.getBoolean("pref_key_enable_vibration", true);
if (doVibrate) {
builder.setVibrate(new long[] {500, 500, 500});
}
boolean playAudio = sharedPrefs.getBoolean("pref_key_enable_sound", true);
if (playAudio) {
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.cp);
builder.setSound(soundUri);
}
setPriority(builder);
builder.setDeleteIntent(getDeleteIntent(context));
return builder;
}
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;
}
private void onSnoozed(Alarm alarm) {
LogUtils.d("onSnoozed: " + alarm);
int alarmCode = (int) alarm.getCode();
Assignment assignment = AssignmentsStore.getInstance().get(alarm.getModelCode());
/*Add action cancel.*/
Intent cancelIntent = new Intent(context, AlarmAlertReceiver.class);
cancelIntent.setAction(Constants.ACTION_CANCEL_NOTIFICATION);
cancelIntent.putExtra(Constants.EXTRA_CODE, alarmCode);
PendingIntent piCancel = PendingIntent.getBroadcast(context, alarmCode, cancelIntent, 0);
/*Action dismiss.*/
PendingIntent piDismiss = PresentationToModelIntents.createPendingIntent(
context, PresentationToModelIntents.ACTION_REQUEST_DISMISS, alarmCode);
Notification status = new Builder(context)
.setContentTitle(assignment.getName())
.setContentText(assignment.getComment())
.setSmallIcon(R.drawable.ic_assignment_turned_in_black_24dp)
.setContentIntent(piCancel)
.setOngoing(true)
.addAction(R.drawable.ic_highlight_off_black_24dp, PalmApp.getStringCompact(R.string.text_dismiss), piDismiss)
.setDefaults(Notification.DEFAULT_LIGHTS)
.build();
notificationManager.notify(alarmCode, status);
}
public NotificationsHelper createNotification(int smallIcon, String title, PendingIntent notifyIntent) {
mBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(smallIcon).setContentTitle(title)
.setAutoCancel(true).setColor(mContext.getResources().getColor(R.color.colorAccent));
mBuilder.setContentIntent(notifyIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setLargeIcon(R.mipmap.ic_launcher_round);
} else {
setLargeIcon(R.mipmap.ic_launcher_round);
}
return this;
}
private void noNewContactNotif(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("No New Contact Found for Backup");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
private void newContactFound(int count){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("Backup of your new "+count+" Contacts succesfully take.");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
private void noNewContactNotif(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("No New Contact Found for Backup");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
private void newContactFound(int count){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_custom_notification);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("Contact Sync!!");
mBuilder.setContentText("Backup of your new "+count+" Contacts succesfully take.");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificaitonId, mBuilder.build());
}
/**
* Displays a notification in the notification area of the UI
* @param context Context from which to create the notification
* @param messageString The string to display to the user as a message
* @param intent The intent which will start the activity when the user clicks the notification
* @param notificationTitle The resource reference to the notification title
*/
static void notifcation(Context context, String messageString, Intent intent, int notificationTitle) {
//Get the notification manage which we will use to display the notification
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
long when = System.currentTimeMillis();
//get the notification title from the application's strings.xml file
CharSequence contentTitle = context.getString(notificationTitle);
//the message that will be displayed as the ticker
String ticker = contentTitle + " " + messageString;
//build the pending intent that will start the appropriate activity
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0, intent, 0);
//build the notification
Builder notificationCompat = new Builder(context);
notificationCompat.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentIntent(pendingIntent)
.setContentText(messageString)
.setTicker(ticker)
.setWhen(when)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = notificationCompat.build();
//display the notification
mNotificationManager.notify(MessageID, notification);
MessageID++;
}
/**
* Return a persistent {@link Notification} for the corresponding id. You
* must return a notification for AT LEAST the first id to be requested.
* Once the persistent notification is shown, further calls to
* {@link #getPersistentNotification(int)} may return null. This way Android
* can start the StandOut window service in the foreground and will not kill
* the service on low memory.
*
* <p>
* As a courtesy, the system will request a notification for every new id
* shown. Your implementation is encouraged to include the
* {@link PendingIntent#FLAG_UPDATE_CURRENT} flag in the notification so
* that there is only one system-wide persistent notification.
*
* <p>
* See the StandOutExample project for an implementation of
* {@link #getPersistentNotification(int)} that keeps one system-wide
* persistent notification that creates a new window on every click.
*
* @param id
* The id of the window.
* @return The {@link Notification} corresponding to the id, or null if
* you've previously returned a notification.
*/
public Notification getPersistentNotification(int id) {
// basic notification stuff
// http://developer.android.com/guide/topics/ui/notifiers/notifications.html
int icon = getAppIcon();
long when = System.currentTimeMillis();
Context c = getApplicationContext();
String contentTitle = getPersistentNotificationTitle(id);
String tickerText = getPersistentNotificationMessage(id);
// getPersistentNotification() is called for every new window
// so we replace the old notification with a new one that has
// a bigger id
Intent notificationIntent = getPersistentNotificationIntent(id);
PendingIntent contentIntent = null;
if (notificationIntent != null) {
contentIntent = PendingIntent.getService(this, 0,
notificationIntent,
// flag updates existing persistent notification
PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(c);
builder.setContentTitle(contentTitle);
builder.setContentText(tickerText);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(icon);
builder.setWhen(when);
return builder.build();
}
/**
* Return a hidden {@link Notification} for the corresponding id. The system
* will request a notification for every id that is hidden.
*
* <p>
* If null is returned, StandOut will assume you do not wish to support
* hiding this window, and will {@link #close(int)} it for you.
*
* <p>
* See the StandOutExample project for an implementation of
* {@link #getHiddenNotification(int)} that for every hidden window keeps a
* notification which restores that window upon user's click.
*
* @param id
* The id of the window.
* @return The {@link Notification} corresponding to the id or null.
*/
public Notification getHiddenNotification(int id) {
// same basics as getPersistentNotification()
int icon = getHiddenIcon();
long when = System.currentTimeMillis();
Context c = getApplicationContext();
String contentTitle = getHiddenNotificationTitle(id);
String contentText = getHiddenNotificationMessage(id);
String tickerText = String.format("%s: %s", contentTitle, contentText);
// the difference here is we are providing the same id
Intent notificationIntent = getHiddenNotificationIntent(id);
PendingIntent contentIntent = null;
if (notificationIntent != null) {
contentIntent = PendingIntent.getService(this, 0,
notificationIntent,
// flag updates existing persistent notification
PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(c);
builder.setContentTitle(contentTitle);
builder.setContentText(tickerText);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(icon);
builder.setWhen(when);
return builder.build();
}
public NotificationBuilder createNotificationService(String title, PendingIntent notifyIntent) {
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_SERVICE_ID,
mContext.getString(R.string.text_scan_foreground_service), NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
mNotificationChannel.setDescription(mContext.getString(R.string.pref_title_background_scan));
mNotificationChannel.enableLights(false);
mNotificationChannel.enableVibration(false);
mNotificationChannel.setSound(null, null);
mNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
notificationManager.createNotificationChannel(mNotificationChannel);
}
mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_SERVICE_ID);
mBuilder.setAutoCancel(true)
.setDefaults(0)
.setSmallIcon(getNotificationSmallIcon())
.setContentTitle(title)
.setColor(ContextCompat.getColor(mContext, R.color.hn_orange));
if (notifyIntent != null) {
mBuilder.setContentIntent(notifyIntent);
}
setLargeIcon(getNotificationLargeIcon());
return this;
}
private void init() {
mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationBuilder = new NotificationCompat.Builder(mContext);
mNotificationBuilder.setContentTitle(title);
mNotificationBuilder.setContentText(text);
if (bigText == null)
mNotificationBuilder.setContentInfo(text);
if (withLargeIcon()) {
mNotificationBuilder.setSmallIcon(small_icon);
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), this.icon);
Bitmap newIcon = Bitmap.createBitmap(icon.getWidth(), icon.getHeight(), icon.getConfig());
Canvas canvas = new Canvas(newIcon);
canvas.drawColor(OResource.color(mContext, R.color.theme_primary));
canvas.drawBitmap(icon, 0, 0, null);
mNotificationBuilder.setLargeIcon(newIcon);
} else {
mNotificationBuilder.setSmallIcon(icon);
}
mNotificationBuilder.setAutoCancel(mAutoCancel);
mNotificationBuilder.setOngoing(mOnGoing);
mNotificationBuilder.setColor(OResource.color(mContext, notification_color));
if (bigText != null) {
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(title);
notiStyle.setSummaryText(text);
notiStyle.bigText(bigText);
mNotificationBuilder.setStyle(notiStyle);
}
if (bigPictureStyle != null) {
mNotificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigPictureStyle));
}
if (maxProgress != -1) {
mNotificationBuilder.setProgress(maxProgress, currentProgress, indeterminate);
}
}
public static Builder getBuilder(Context context, boolean alert)
{
Builder builder = new Builder(context).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true);
if (alert == true)
{
int defaults = 0;
if (KlyphPreferences.getNotificationRingtone() != null && KlyphPreferences.getNotificationRingtone().equals("default"))
{
defaults |= android.app.Notification.DEFAULT_SOUND;
}
else if (KlyphPreferences.getNotificationRingtoneUri() == null)
{
builder.setSound(null);
}
else
{
builder.setSound(Uri.parse(KlyphPreferences.getNotificationRingtoneUri()));
}
if (KlyphPreferences.isNotificationVibrationEnabled() == true)
defaults |= android.app.Notification.DEFAULT_VIBRATE;
defaults |= android.app.Notification.DEFAULT_LIGHTS;
builder.setDefaults(defaults);
builder.setOnlyAlertOnce(true);
}
return builder;
}
public static void setInboxStyle(Builder builder, String title, List<String> lines)
{
builder.setNumber(lines.size());
InboxStyle inboxStyle = new InboxStyle();
inboxStyle.setBigContentTitle(title);
for (String line : lines)
{
inboxStyle.addLine(line);
}
builder.setStyle(inboxStyle);
}
private void sendNotification(final Notification notification, final boolean sendDetails)
{
final Builder builder = KlyphNotification.getBuilder(service.get(), true);
builder.setContentTitle(notification.getSender_name());
builder.setContentText(notification.getTitle_text());
builder.setTicker(String.format("%1$s\n%2$s", notification.getSender_name(), notification.getTitle_text()));
ImageLoader.loadImage(notification.getSender_pic(), new SimpleFakeImageLoaderListener() {
@Override
public void onBitmapFailed(Drawable drawable)
{
if (sendDetails)
KlyphNotification.sendNotification(service.get(), builder, notification);
else
KlyphNotification.sendNotification(service.get(), builder);
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1)
{
builder.setLargeIcon(bitmap);
if (sendDetails)
KlyphNotification.sendNotification(service.get(), builder, notification);
else
KlyphNotification.sendNotification(service.get(), builder);
}
});
}
private void init() {
mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationBuilder = new NotificationCompat.Builder(mContext);
mNotificationBuilder.setContentTitle(title);
mNotificationBuilder.setContentText(text);
if (bigText == null)
mNotificationBuilder.setContentInfo(text);
if (withLargeIcon()) {
mNotificationBuilder.setSmallIcon(small_icon);
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), this.icon);
Bitmap newIcon = Bitmap.createBitmap(icon.getWidth(), icon.getHeight(), icon.getConfig());
Canvas canvas = new Canvas(newIcon);
canvas.drawColor(OResource.color(mContext, R.color.theme_primary));
canvas.drawBitmap(icon, 0, 0, null);
mNotificationBuilder.setLargeIcon(newIcon);
} else {
mNotificationBuilder.setSmallIcon(icon);
}
mNotificationBuilder.setAutoCancel(mAutoCancel);
mNotificationBuilder.setOngoing(mOnGoing);
mNotificationBuilder.setColor(OResource.color(mContext, notification_color));
if (bigText != null) {
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(title);
notiStyle.setSummaryText(text);
notiStyle.bigText(bigText);
mNotificationBuilder.setStyle(notiStyle);
}
if (bigPictureStyle != null) {
mNotificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigPictureStyle));
}
if (maxProgress != -1) {
mNotificationBuilder.setProgress(maxProgress, currentProgress, indeterminate);
}
}
private ChatNotificationHelper(Context context) {
//Private Constructor
this();
mNotificationBuilder = new Builder(context);
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationSoundUri = getUserSelectedSoundUri();
mNotificationsEnabled = getNotificationsEnabled();
mVibrationEnabled = getVibrationEnabled();
}
/**
* Handle command
*
* @param intent
*/
private void handleCommand(Intent intent) {
if( ACTION_FOREGROUND.equals(intent.getAction()) ) {
if( foreground ) {
return; // XXX: We are already in foreground...
}
foreground = true;
Intent notifyIntent = new Intent(this, ServersActivity.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
// Set the icon, scrolling text and timestamp
// now using NotificationCompat for Linter happiness
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_service_icon)
.setWhen(System.currentTimeMillis())
.setContentText(getText(R.string.notification_running))
.setTicker(getText(R.string.notification_not_connected))
.setContentTitle(getText(R.string.app_name))
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_MIN)
.build();
startForegroundCompat(FOREGROUND_NOTIFICATION, notification);
} else if( ACTION_BACKGROUND.equals(intent.getAction()) && !foreground ) {
stopForegroundCompat(FOREGROUND_NOTIFICATION);
} else if( ACTION_ACK_NEW_MENTIONS.equals(intent.getAction()) ) {
ackNewMentions(intent.getIntExtra(EXTRA_ACK_SERVERID, -1), intent.getStringExtra(EXTRA_ACK_CONVTITLE));
}
}
private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) {
final Resources resources = mXmppConnectionService.getResources();
final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone));
final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification));
final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
if (notify && !quietHours) {
if (vibrate) {
final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat};
mBuilder.setVibrate(pattern);
} else {
mBuilder.setVibrate(new long[]{0});
}
Uri uri = Uri.parse(ringtone);
try {
mBuilder.setSound(fixRingtoneUri(uri));
} catch (SecurityException e) {
Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
}
} else {
mBuilder.setLocalOnly(true);
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}
mBuilder.setPriority(notify ? (headsup ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT) : NotificationCompat.PRIORITY_LOW);
setNotificationColor(mBuilder);
mBuilder.setDefaults(0);
if (led) {
mBuilder.setLights(LED_COLOR, 2000, 3000);
}
}
private void modifyIncomingCall(Builder mBuilder) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final Resources resources = mXmppConnectionService.getResources();
final String ringtone = preferences.getString("call_ringtone", resources.getString(R.string.incoming_call_ringtone));
mBuilder.setVibrate(CALL_PATTERN);
final Uri uri = Uri.parse(ringtone);
try {
mBuilder.setSound(fixRingtoneUri(uri));
} catch (SecurityException e) {
Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
}
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
setNotificationColor(mBuilder);
mBuilder.setLights(LED_COLOR, 2000, 3000);
}
private void modifyForImage(final Builder builder, final Message message, final ArrayList<Message> messages) {
try {
final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false);
final ArrayList<Message> tmp = new ArrayList<>();
for (final Message msg : messages) {
if (msg.getType() == Message.TYPE_TEXT
&& msg.getTransferable() == null) {
tmp.add(msg);
}
}
final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap);
if (tmp.size() > 0) {
CharSequence text = getMergedBodies(tmp);
bigPictureStyle.setSummaryText(text);
builder.setContentText(text);
builder.setTicker(text);
} else {
final String description = UIHelper.getFileDescriptionString(mXmppConnectionService, message);
builder.setContentText(description);
builder.setTicker(description);
}
builder.setStyle(bigPictureStyle);
} catch (final IOException e) {
modifyForTextOnly(builder, messages);
}
}