/** Notifies the user that a backup was made */ private void onBackupComplete() { CharSequence noticeText = "Translations backed up"; // activity to open when clicked // TODO: instead of the home activity we need a backup activity where the user can view their // backups. Intent notificationIntent = new Intent(getApplicationContext(), HomeActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); // build notification NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_notify_msg) .setContentTitle(noticeText) .setAutoCancel(true) .setContentIntent(intent) .setNumber(0); // build big notification NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle(noticeText); mBuilder.setStyle(inboxStyle); // issue notification NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mNotifyMgr.notify(0, mBuilder.build()); } else { mNotifyMgr.notify(0, mBuilder.getNotification()); } }
/** Show as local notification when in background. */ @SuppressWarnings("deprecation") private void showNotification() { int id = getOptions().getIdAsInt(); if (Build.VERSION.SDK_INT <= 15) { // Notification for HoneyComb to ICS getNotMgr().notify(id, builder.getNotification()); } else { // Notification for Jellybean and above getNotMgr().notify(id, builder.build()); } }
@Subscribe public void onNewIncomingMessage(NewIncomingMessageEvent event) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("New message"); builder.setContentText( event.getMessage().getContact().getName() + " : " + event.getMessage().getBody()); builder.setSmallIcon(R.drawable.ic_menu_send); builder.setAutoCancel(true); Intent intent = new Intent(context, ConversationActivity.class); intent.putExtra(Const.EMAIL, event.getEmail()); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pendingIntent); notificationManager.notify( BASE_NOTIFICATON_ID + Long.valueOf(event.getMessage().getId()).intValue(), builder.getNotification()); }
/** * Updates the contents of the service's notifications. Should be called before * setupNotificationBuilders. */ @SuppressLint("NewApi") private Notification updateNotifications() { String contentTitle = getString(R.string.download_notification_title); String downloadsLeft = requester.getNumberOfDownloads() + getString(R.string.downloads_left); if (android.os.Build.VERSION.SDK_INT >= 16) { if (notificationBuilder != null) { StringBuilder bigText = new StringBuilder(""); for (int i = 0; i < downloads.size(); i++) { Downloader downloader = downloads.get(i); final DownloadRequest request = downloader.getDownloadRequest(); if (request.getFeedfileType() == Feed.FEEDFILETYPE_FEED) { if (request.getTitle() != null) { if (i > 0) { bigText.append("\n"); } bigText.append("\u2022 " + request.getTitle()); } } else if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { if (request.getTitle() != null) { if (i > 0) { bigText.append("\n"); } bigText.append( "\u2022 " + request.getTitle() + " (" + request.getProgressPercent() + "%)"); } } } notificationBuilder.setSummaryText(downloadsLeft); notificationBuilder.setBigContentTitle(contentTitle); if (bigText != null) { notificationBuilder.bigText(bigText.toString()); } return notificationBuilder.build(); } } else { if (notificationCompatBuilder != null) { notificationCompatBuilder.setContentTitle(contentTitle); notificationCompatBuilder.setContentText(downloadsLeft); return notificationCompatBuilder.getNotification(); } } return null; }
/** Notify user about running server and connected clients */ public synchronized void notifyUser(String title, String message, boolean ongoing) { PendingIntent intent = PendingIntent.getActivity(AppContext, 0, new Intent(AppContext, main.class), 0); String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) AppContext.getSystemService(ns); NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(AppContext); notificationbuilder.setContentTitle(title); notificationbuilder.setContentText(message); notificationbuilder.setTicker(message); notificationbuilder.setSmallIcon(R.drawable.ic_launcher); notificationbuilder.setContentIntent(intent); notificationbuilder.setOngoing(ongoing); Notification notification = notificationbuilder.getNotification(); mNotificationManager.notify(1, notification); }
private void setupNotification() { try { NotificationCompat.Builder builder = new NotificationCompat.Builder(ContextManager.getContext()); builder .setContentText(ContextManager.getString(R.string.actfm_sync_ongoing)) .setContentTitle(ContextManager.getString(R.string.app_name)) .setOngoing(true) .setSmallIcon(android.R.drawable.stat_notify_sync) .setContentIntent( PendingIntent.getActivity( ContextManager.getContext().getApplicationContext(), 0, new Intent(), 0)); notificationManager.notify(0, builder.getNotification()); notificationId = 0; } catch (Exception e) { Log.e(ERROR_TAG, "Exception creating notification", e); // $NON-NLS-1$ } catch (Error e) { Log.e(ERROR_TAG, "Error creating notification", e); // $NON-NLS-1$ } }
/** * Build notification info * * @param context The calling activity * @param drawable The notification icon * @param message The message * @param title The title for the notification * @param intent The pending intent * @param ongoing True if you don't want the user to clear the notification */ public static void buildNotification( Context context, int drawable, String message, String title, PendingIntent intent, boolean ongoing) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle(title); builder.setContentText(message); builder.setSmallIcon(drawable); builder.setContentIntent(intent); if (ongoing) { builder.setOngoing(ongoing); } notificationManager.notify(NOTIFY_RUNNING, builder.getNotification()); }
protected void displayNotificationOne() { // Invoking the default notification service // NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( // this); NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // mBuilder.setContentTitle("ATENCION!!!"); // // mBuilder.setContentText("Atención su bateria esta por debajo del 20%."); // // mBuilder.setTicker("Busque su centro de recarga mas cercano."); // mBuilder.setSmallIcon(R.drawable.ic_logo_bc); // mBuilder.setSound(soundUri); // Increase notification number every time a new notification arrives // mBuilder.setNumber(++numMessagesOne); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, TabsUsuario.class); resultIntent.putExtra("battery", battery); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); // This ensures that navigating backward from the Activity leads out of // the app to Home page NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) // .setSmallIcon(R.drawable.ic_stat_gcm) .setContentTitle("Be Connected") .setSmallIcon(R.drawable.ic_logo_bc) .setStyle( new NotificationCompat.BigTextStyle() .bigText("Atención su bateria esta por debajo del 20%.")) .setAutoCancel(true) .setSound(soundUri) .setContentText("Busque su centro de recarga mas cercano."); mBuilder.setContentIntent(contentIntent); mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(0, mBuilder.build()); // TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent // stackBuilder.addParentStack(TabsUsuario.class); // Adds the Intent that starts the Activity to the top of the stack // stackBuilder.addNextIntent(resultIntent); // PendingIntent resultPendingIntent = // stackBuilder.getPendingIntent( // 0, // PendingIntent.FLAG_ONE_SHOT // can only be used once // ); // start the activity when the user clicks the notification text // mBuilder.setContentIntent(resultPendingIntent); // mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; // myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // pass the Notification object to the system // myNotificationManager.notify(notificationIdOne, mBuilder.build()); }