Java Code Examples for android.support.v4.app.NotificationCompat.Builder#build()

The following examples show how to use android.support.v4.app.NotificationCompat.Builder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Notify.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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++;

}
 
Example 2
Source File: AlarmAlertReceiver.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
private void onAlert(Alarm alarm) {
    int alarmCode = (int) alarm.getCode();
    Assignment assignment = AssignmentsStore.getInstance().get(alarm.getModelCode());
    /*If the assignment is null, we don't show notification for it*/
    if (assignment == null) {
        alarmsManager.removeAlarm(alarm);
        return;
    }

    Builder mBuilder = new Builder(context)
            .setContentTitle(assignment.getName())
            .setContentText(assignment.getComment())
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_assignment_turned_in_black_24dp)
            .setColor(ColorUtils.accentColor());

    /*Add action: mark as done.*/
    Intent mdIntent = new Intent(context, AlarmAlertReceiver.class);
    mdIntent.setAction(Constants.ACTION_MARK_ASSIGNMENT_AS_DONE);
    mdIntent.putExtra(Constants.EXTRA_CODE, assignment.getCode());
    mdIntent.putExtra(Constants.EXTRA_NOTIFICATION_ID, (int) alarm.getCode());
    PendingIntent piMD = PendingIntent.getBroadcast(
            context, (int) alarm.getCode(), mdIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    mBuilder.addAction(R.drawable.ic_check_circle_black_24dp, context.getString(R.string.mark_as_done), piMD);

    /*Add action: postpone.*/
    Intent pIntent = new Intent(context, AlarmAlertReceiver.class);
    pIntent.setAction(Constants.ACTION_POSTPONE_ALARM);
    pIntent.putExtra(Constants.EXTRA_CODE, alarm.getCode());
    pIntent.putExtra(Constants.EXTRA_NOTIFICATION_ID, (int) alarm.getCode());
    PendingIntent piP = PendingIntent.getBroadcast(
            context, (int) alarm.getCode(), pIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    int snoozeMinutes = NoticePreferences.getInstance().getSnoozeDuration();
    mBuilder.addAction(R.drawable.ic_snooze_black_24dp, String.format(context.getString(R.string.remind_in_minutes), snoozeMinutes), piP);

    /*Add action: click.*/
    Intent clickIntent = new Intent(context, ContentActivity.class);
    clickIntent.setAction(Constants.ACTION_NOTIFICATION);
    clickIntent.putExtra(Constants.EXTRA_CODE, assignment.getCode());
    clickIntent.putExtra(Constants.EXTRA_FRAGMENT, Constants.VALUE_FRAGMENT_ASSIGNMENT);
    clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(
            context, (int) assignment.getCode(), clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(contentIntent);

    /*Vibrate.*/
    if (NoticePreferences.getInstance().isVibrateAllowed()) {
        mBuilder.setVibrate(getVibrate());
    }

    /*Ringtone.*/
    String ringtone = NoticePreferences.getInstance().getNotificationRingtone(); // 铃声
    if (ringtone != null){
        mBuilder.setSound(Uri.parse(ringtone));
    }

    Notification notification = mBuilder.build();

    /*Light.*/
    setNotificationLight(notification);

    notificationManager.notify(alarmCode, notification);
}
 
Example 3
Source File: SipNotifications.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
public synchronized void notifyRegisteredAccounts(ArrayList<SipProfileState> activeAccountsInfos, boolean showNumbers) {
	if (!isServiceWrapper) {
		Log.e(THIS_FILE, "Trying to create a service notification from outside the service");
		return;
	}
	int icon = R.drawable.ic_stat_sipok;
	CharSequence tickerText = context.getString(R.string.service_ticker_registered_text);
	long when = System.currentTimeMillis();
	

       Builder nb = new Builder(context);
       nb.setSmallIcon(icon);
       nb.setTicker(tickerText);
       nb.setWhen(when);
	Intent notificationIntent = new Intent(SipManager.ACTION_SIP_DIALER);
	notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	
	RegistrationNotification contentView = new RegistrationNotification(context.getPackageName());
	contentView.clearRegistrations();
	if(!Compatibility.isCompatible(9)) {
	    contentView.setTextsColor(notificationPrimaryTextColor);
	}
	contentView.addAccountInfos(context, activeAccountsInfos);

	// notification.setLatestEventInfo(context, contentTitle,
	// contentText, contentIntent);
	nb.setOngoing(true);
	nb.setOnlyAlertOnce(true);
       nb.setContentIntent(contentIntent);
       nb.setContent(contentView);
	
	Notification notification = nb.build();
	notification.flags |= Notification.FLAG_NO_CLEAR;
	// We have to re-write content view because getNotification setLatestEventInfo implicitly
       notification.contentView = contentView;
	if (showNumbers) {
           // This only affects android 2.3 and lower
           notification.number = activeAccountsInfos.size();
       }
	startForegroundCompat(REGISTER_NOTIF_ID, notification);
}
 
Example 4
Source File: IMNotificationManager.java    From sctalk with Apache License 2.0 4 votes vote down vote up
private void showInNotificationBar(String title,String ticker, Bitmap iconBitmap,int notificationId,Intent intent) {
	logger.d("notification#showInNotificationBar title:%s ticker:%s",title,ticker);

	NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
	if (notifyMgr == null) {
		return;
	}

	Builder builder = new NotificationCompat.Builder(ctx);
	builder.setContentTitle(title);
	builder.setContentText(ticker);
	builder.setSmallIcon(R.drawable.tt_small_icon);
	builder.setTicker(ticker);
	builder.setWhen(System.currentTimeMillis());
	builder.setAutoCancel(true);

	// this is the content near the right bottom side
	// builder.setContentInfo("content info");

	if (configurationSp.getCfg(SysConstant.SETTING_GLOBAL,ConfigurationSp.CfgDimension.VIBRATION)) {
		// delay 0ms, vibrate 200ms, delay 250ms, vibrate 200ms
		long[] vibrate = {0, 200, 250, 200};
		builder.setVibrate(vibrate);
	} else {
		logger.d("notification#setting is not using vibration");
	}

	// sound
	if (configurationSp.getCfg(SysConstant.SETTING_GLOBAL,ConfigurationSp.CfgDimension.SOUND)) {
		builder.setDefaults(Notification.DEFAULT_SOUND);
	} else {
		logger.d("notification#setting is not using sound");
	}
	if (iconBitmap != null) {
		logger.d("notification#fetch icon from network ok");
		builder.setLargeIcon(iconBitmap);
	} else {
           // do nothint ?
	}
	// if MessageActivity is in the background, the system would bring it to
	// the front
	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	PendingIntent pendingIntent = PendingIntent.getActivity(ctx, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	builder.setContentIntent(pendingIntent);
	Notification notification = builder.build();
	notifyMgr.notify(notificationId, notification);
}