/** Update the notification with the new track info */ private void updateNotification() { if (mLastSong != null) { Bitmap scaledArt = Bitmap.createScaledBitmap( mLastSong.getArt(), mNotificationWidth, mNotificationHeight, false); mNotifyBuilder.setLargeIcon(scaledArt); mNotifyBuilder.setContentTitle(mLastSong.getArtist()); mNotifyBuilder.setContentText(mLastSong.getTitle() + " / " + mLastSong.getAlbum()); } else { mNotifyBuilder.setContentTitle(App.mApp.getString(R.string.app_name)); mNotifyBuilder.setContentText(App.mApp.getString(R.string.player_nosong)); } mNotificationManager.notify(App.NOTIFY_ID, mNotifyBuilder.build()); }
private Notification makeNotification( PendingIntent pendingIntent, String title, String content, String tickerText, int iconId, boolean ring, boolean vibrate) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder .setContentTitle(title) .setContentText(content) .setAutoCancel(true) .setContentIntent(pendingIntent) .setTicker(tickerText) .setSmallIcon(iconId); int defaults = Notification.DEFAULT_LIGHTS; if (vibrate) { defaults |= Notification.DEFAULT_VIBRATE; } if (ring) { defaults |= Notification.DEFAULT_SOUND; } builder.setDefaults(defaults); return builder.build(); }
private void init() { builder .setContentTitle(getString(R.string.app_name)) .setProgress(0, 0, true) .setContentText("Preparing to download databases") .setSmallIcon(R.drawable.ic_stat_maps_local_library); }
@Override public Notification createNotification(String title, String content, int icon) { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(content); notificationBuilder.setSmallIcon(icon); notificationBuilder.setAutoCancel(true); Intent resultIntent = NotificationDetailsActivity.getStartIntent(context, title, content); // The stack builder object will contain an artificial back stack for // the started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(NotificationDetailsActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(resultPendingIntent); return notificationBuilder.build(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent i = new Intent(getIntent()); nome = i.getIntExtra("nome", nome); foto = i.getIntExtra("foto", foto); id = UUID.fromString(i.getStringExtra("id")); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle(getString(R.string.novo_evento)); builder.setContentText(getString(nome)); builder.setTicker(getString(R.string.notificacao_evento)); builder.setSmallIcon(foto); // TODO colocar rodar mesmo com app fechado Intent resultIntent = new Intent(this, EventoPagerActivity.class); resultIntent.putExtra(EventoFragment.EXTRA_EVENTO_ID, id); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(resultPendingIntent); builder.setAutoCancel(true); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(idNotificacao, builder.build()); finish(); }
private NotificationCompat.Builder createDefaultNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); Intent onClick = new Intent(); onClick.setClassName( getPackageName(), /*Aptoide.getConfiguration().*/ getStartActivityClass().getName()); // TODO dependency injection onClick.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); onClick.setAction(Intent.ACTION_VIEW); onClick.putExtra("fromDownloadNotification", true); // The PendingIntent to launch our activity if the user selects this notification PendingIntent onClickAction = PendingIntent.getActivity( getApplicationContext(), 0, onClick, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setOngoing(true); mBuilder .setContentTitle( getString(R.string.aptoide_downloading, Aptoide.getConfiguration().getMarketName())) .setSmallIcon(R.drawable.stat_sys_download) .setProgress(0, 0, true) .setContentIntent(onClickAction); mBuilder.setProgress(100, 0, true); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)) .notify(-3, mBuilder.build()); return mBuilder; }
/** * 当应用在前台时,如果当前消息不是属于当前会话,在状态栏提示一下 如果不需要,注释掉即可 * * @param message */ protected void notifyNewMessage(EMMessage message, String nick) { // 如果是设置了不提醒只显示数目的群组(这个是app里保存这个数据的,demo里不做判断) // 以及设置了setShowNotificationInbackgroup:false(设为false后,后台时sdk也发送广播) if (!EasyUtils.isAppRunningForeground(this)) { return; } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(getApplicationInfo().icon) .setWhen(System.currentTimeMillis()) .setAutoCancel(false); String ticker = CommonUtils.getMessageDigest(message, this); String st = getResources().getString(R.string.expression); if (message.getType() == EMMessage.Type.TXT) ticker = ticker.replaceAll("\\[.{2,3}\\]", st); // 设置状态栏提示 mBuilder.setTicker(nick + ": " + ticker); mBuilder.setContentText("查看新的未读消息"); mBuilder.setContentTitle(message.getStringAttribute(Const.Easemob.FROM_USER_NICK, "新消息")); // 必须设置pendingintent,否则在2.3的机器上会有bug Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Const.Intent.HX_NTF_TO_MAIN, true); PendingIntent pendingIntent = PendingIntent.getActivity(this, notifiId, intent, PendingIntent.FLAG_ONE_SHOT); mBuilder.setContentIntent(pendingIntent); Notification notification = mBuilder.build(); notificationManager.notify(notifiId, notification); // notificationManager.cancel(notifiId); }
// 发送简单通知 private NotificationCompat.Builder sendSimpleNotification(NotificationCompat.Builder builder) { builder.setTicker("通知来了"); builder.setContentTitle("新消息"); builder.setContentText("要放假了"); return builder; }
private void startAsForeground() { NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); int icon; if (Build.VERSION.SDK_INT > 21) icon = R.mipmap.ic_test; else icon = R.mipmap.ic_test103; mBuilder .setContentTitle("Stats Collector") .setContentText("Started: " + dateFormat.format(date)) .setSubText("Session: " + counter) .setContentInfo("Interval: " + Settings.interval) .setSmallIcon(icon) .setColor(Color.parseColor("#78909C")) .setCategory(Notification.CATEGORY_SERVICE) .setPriority(Notification.PRIORITY_LOW) .setContentIntent(resultPendingIntent); // .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_test1)); startForeground(ONGOING_NOTIFICATION_ID, mBuilder.build()); isForeground = true; }
@Override protected Notification fillNotification(NotificationCompat.Builder notificationBuilder) { NotificationCompat.WearableExtender morePageNotification = new NotificationCompat.WearableExtender(); String firstContent = "", firstTime = ""; for (TransportManager.Departure d : mDepartures) { if (firstTime.isEmpty()) { firstTime = d.countDown + "min"; firstContent = d.servingLine; } NotificationCompat.Builder pageNotification = new NotificationCompat.Builder(mContext) .setContentTitle(d.countDown + "min") .setContentText(d.servingLine); morePageNotification.addPage(pageNotification.build()); } notificationBuilder.setContentTitle(firstTime); notificationBuilder.setContentText(firstContent); Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.wear_mvv); morePageNotification.setBackground(bm); return morePageNotification.extend(notificationBuilder).build(); }
@Override public void vaddLogNotif(final Context ctxt, final boolean flag) { if (!flag) { vcancel(ctxt, LOG_TAG, NotifUtil.LOGNOTIFID); return; } NotificationCompat.Builder builder = new NotificationCompat.Builder(ctxt); Intent intent = new Intent(ctxt, WifiFixerActivity.class) .setAction(Intent.ACTION_MAIN) .setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); builder.setContentIntent(PendingIntent.getActivity(ctxt, 0, intent, 0)); builder.setOngoing(true); builder.setOnlyAlertOnce(true); builder.setOngoing(true); builder.setSmallIcon(R.drawable.logging_enabled); builder.setContentTitle(ctxt.getString(R.string.logservice)); builder.setContentText(getLogString(ctxt).toString()); /* * Fire the notification */ notify(ctxt, NotifUtil.LOGNOTIFID, LOG_TAG, builder.build()); }
@Override public void vshow( final Context context, final String message, final String tickerText, final int id, PendingIntent contentIntent) { /* * If contentIntent is NULL, create valid contentIntent */ if (contentIntent == null) contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setTicker(tickerText); builder.setWhen(System.currentTimeMillis()); builder.setSmallIcon(R.drawable.icon); builder.setContentTitle(context.getText(R.string.app_name)); builder.setContentIntent(contentIntent); builder.setContentText(message); builder.setAutoCancel(true); // unique ID notify(context, id, VSHOW_TAG, builder.build()); }
public void notificar( int idNotificacao, String titulo, String texto, String ticker, Class classe, Parada parada, int icone) { mBuilder.setContentTitle(titulo).setContentText(texto).setTicker(ticker); mBuilder.setSmallIcon(icone); Intent resultIntent = new Intent(con, classe); resultIntent.putExtra("nomeparada", parada.getNome()); resultIntent.putExtra("codigoparada", parada.getCodigoParada()); SimpleDateFormat dateFormat_hora = new SimpleDateFormat("HH:mm:ss"); Date data = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(data); Date data_atual = cal.getTime(); String hora_atual = dateFormat_hora.format(data_atual); resultIntent.putExtra("infotempoatual", hora_atual); TaskStackBuilder stackBuilder = TaskStackBuilder.create(con); stackBuilder.addParentStack(classe); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); mNotificationManager.notify(idNotificacao, mBuilder.build()); }
public void updateNotification() { if (!Preferences.isNotificationEnabled() || !playServicesAvailable) return; String title = null; String subtitle = null; long time = 0; if ((this.lastPublishedLocation != null) && Preferences.isNotificationLocationEnabled()) { time = this.lastPublishedLocationTime.getTime(); if ((this.lastPublishedLocation.getGeocoder() != null) && Preferences.isNotificationGeocoderEnabled()) { title = this.lastPublishedLocation.toString(); } else { title = this.lastPublishedLocation.toLatLonString(); } } else { title = this.context.getString(R.string.app_name); } subtitle = ServiceLocator.getStateAsString(this.context) + " | " + ServiceBroker.getStateAsString(this.context); notificationBuilder.setContentTitle(title); notificationBuilder .setSmallIcon(R.drawable.ic_notification) .setContentText(subtitle) .setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MIN); if (time != 0) notificationBuilder.setWhen(this.lastPublishedLocationTime.getTime()); this.notification = notificationBuilder.build(); this.context.startForeground(Defaults.NOTIFCATION_ID, this.notification); }
@Override public void onReceive(Context context, Intent i) { Log.d("alarm", "alarm ring! ring!"); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mCompatBuilder = new NotificationCompat.Builder(context); switch (i.getIntExtra("sound", 0)) { case 0: if ("ON".equals(SharedPreferenceManager.getInstance().getSound())) { mCompatBuilder.setDefaults(Notification.DEFAULT_SOUND); } else { mCompatBuilder.setDefaults(Notification.DEFAULT_LIGHTS); } break; case 1: if ("ON".equals(SharedPreferenceManager.getInstance().getVibration())) { mCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE); } else { mCompatBuilder.setDefaults(Notification.DEFAULT_LIGHTS); } break; case 2: mCompatBuilder.setDefaults(Notification.DEFAULT_LIGHTS); break; } mCompatBuilder.setSmallIcon(R.drawable.ic_launcher); mCompatBuilder.setTicker(i.getStringExtra("ticker")); mCompatBuilder.setContentTitle(i.getStringExtra("title")); mCompatBuilder.setContentText(i.getStringExtra("text")); mCompatBuilder.setAutoCancel(true); nm.notify(0, mCompatBuilder.build()); }
/** * 在notifiaction中显示进度条 * * @return */ private NotificationCompat.Builder displayProgressInNotification() { // TODO 这个图片不设置的话,通知出不来 builder.setSmallIcon(R.drawable.icon); builder.setContentTitle("下载文件"); builder.setContentText("正在下载。。。"); new Thread( new Runnable() { @Override public void run() { // TODO <=100 for (int i = 0; i <= 100; i += 5) { // 将setProgress的第三个参数设为true即可显示为无明确进度的进度条样式,而不是没有进度条 builder.setProgress(100, i, false); manager.notify(0, builder.build()); try { Thread.sleep(5 * 100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } builder.setContentText("下载完成"); builder.setProgress(0, 0, false); // id相同,使用新设置的属性的通知,代替之前的通知 manager.notify(0, builder.build()); } }) .start(); return builder; }
@Override public void onReceive(Context context, Intent bootIntent) { // Check if enabled if (!Utils.isXposedEnabled()) { // Create Xposed installer intent Intent xInstallerIntent = context.getPackageManager().getLaunchIntentForPackage("de.robv.android.xposed.installer"); if (xInstallerIntent != null) xInstallerIntent.putExtra("opentab", 1); PendingIntent pi = (xInstallerIntent == null ? null : PendingIntent.getActivity( context, 0, xInstallerIntent, PendingIntent.FLAG_UPDATE_CURRENT)); // Build notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context); notificationBuilder.setSmallIcon(R.drawable.ic_launcher); notificationBuilder.setContentTitle(context.getString(R.string.app_name)); notificationBuilder.setContentText("PeerBlock For Android is not enabled in XPosed"); notificationBuilder.setWhen(System.currentTimeMillis()); notificationBuilder.setAutoCancel(true); if (pi != null) notificationBuilder.setContentIntent(pi); Notification notification = notificationBuilder.build(); // Display notification NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notification); } }
protected Notification getNotification(Context context, Intent intent) { JSONObject pushData = getPushData(intent); if (pushData == null || (!pushData.has("alert") && !pushData.has("title"))) { return null; } String title = pushData.optString("title", getDisplayName(context)); String alert = pushData.optString("alert", "Notification received."); String tickerText = String.format(Locale.getDefault(), "%s: %s", title, alert); Bundle extras = intent.getExtras(); if (pushData.has("def")) { extras.putString(Constant.APP_DEF_TAB, pushData.optString("def", null)); } Random random = new Random(); int contentIntentRequestCode = random.nextInt(); int deleteIntentRequestCode = random.nextInt(); // Security consideration: To protect the app from tampering, we require that intent filters // not be exported. To protect the app from information leaks, we restrict the packages which // may intercept the push intents. String packageName = context.getPackageName(); Intent contentIntent = new Intent(ParsePushBroadcastReceiver.ACTION_PUSH_OPEN); contentIntent.putExtras(extras); contentIntent.setPackage(packageName); Intent deleteIntent = new Intent(ParsePushBroadcastReceiver.ACTION_PUSH_DELETE); deleteIntent.putExtras(extras); deleteIntent.setPackage(packageName); PendingIntent pContentIntent = PendingIntent.getBroadcast( context, contentIntentRequestCode, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pDeleteIntent = PendingIntent.getBroadcast( context, deleteIntentRequestCode, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT); // The purpose of setDefaults(Notification.DEFAULT_ALL) is to inherit notification properties // from system defaults NotificationCompat.Builder parseBuilder = new NotificationCompat.Builder(context); parseBuilder .setContentTitle(title) .setContentText(alert) .setTicker(tickerText) .setSmallIcon(this.getSmallIconId(context, intent)) .setLargeIcon(getLargeIcon(context)) .setContentIntent(pContentIntent) .setDeleteIntent(pDeleteIntent) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL); if (alert != null && alert.length() > ParsePushBroadcastReceiver.SMALL_NOTIFICATION_MAX_CHARACTER_LIMIT) { parseBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(alert)); } return parseBuilder.build(); }
private void initBuilder(String title, String content) { builder = new NotificationCompat.Builder(baseActivity.getBaseContext()); builder.setContentTitle(title); builder.setContentText(content); builder.setSmallIcon(R.drawable.ic_lets_go); builder.setPriority(NotificationCompat.PRIORITY_MAX); builder.setOngoing(true); }
private void updateNotificationContent() { builder.setContentTitle(songTitle); builder.setContentText(songArtist); builder.setContentInfo( String.format(context.getString(R.string.notification_drink), this.currentMinute)); builder.setLargeIcon(coverArt); notificationManager.notify(notificationId, builder.build()); }
/** 暂停下载 */ public void pauseDownloadNotify() { isPause = true; if (!isCustom) { mBuilder.setContentTitle("已暂停"); setNotify(progress); } else { showCustomProgressNotify("已暂停"); } }
private void postExecute() { taskIsRunning = false; builder.setContentTitle("Search complete!"); builder.setContentText("Jimp3 found " + songsAdded + " new songs."); // Removes the progress bar builder.setProgress(0, 0, false); notification.notify(id, builder.build()); stopSelf(); }
/** 显示通知栏 */ public void showNotify() { mBuilder .setContentTitle("测试标题") .setContentText("测试内容") // .setNumber(number)//显示数量 .setTicker("测试通知来啦"); // 通知首次出现在通知栏,带上升动画效果的 mNotificationManager.notify(11, mBuilder.build()); // mNotification.notify(getResources().getString(R.string.app_name), notiId, mBuilder.build()); }
public void startNotification() { builder.setSmallIcon(R.drawable.ic_stat_name); builder.setContentIntent(pendingIntent); builder.setAutoCancel(false); builder.setOngoing(true); builder.setContentTitle("Job Timer"); builder.setContentText("00:00:00"); notificationManager.notify(NOTIFICATION_ID, builder.build()); }
private void publishResults( String a, int p1, int p2, int id, long total, long done, boolean b, boolean move) { if (hash.get(id)) { mBuilder.setProgress(100, p1, false); mBuilder.setOngoing(true); int title = R.string.copying; if (move) title = R.string.moving; mBuilder.setContentTitle(utils.getString(c, title)); mBuilder.setContentText( new File(a).getName() + " " + utils.readableFileSize(done) + "/" + utils.readableFileSize(total)); int id1 = Integer.parseInt("456" + id); mNotifyManager.notify(id1, mBuilder.build()); if (p1 == 100 || total == 0) { mBuilder.setContentTitle("Copy completed"); if (move) mBuilder.setContentTitle("Move Completed"); mBuilder.setContentText(""); mBuilder.setProgress(0, 0, false); mBuilder.setOngoing(false); mBuilder.setAutoCancel(true); mNotifyManager.notify(id1, mBuilder.build()); publishCompletedResult(id, id1); } DataPackage intent = new DataPackage(); intent.setName(a); intent.setTotal(total); intent.setDone(done); intent.setId(id); intent.setP1(p1); intent.setP2(p2); intent.setMove(move); intent.setCompleted(b); hash1.put(id, intent); if (progressListener != null) { progressListener.onUpdate(intent); if (b) progressListener.refresh(); } } else publishCompletedResult(id, Integer.parseInt("456" + id)); }
/** 显示带进度条通知栏 */ public void showProgressNotify() { mBuilder.setContentTitle("等待下载").setContentText("进度:").setTicker("开始下载"); // 通知首次出现在通知栏,带上升动画效果的 if (indeterminate) { // 不确定进度的 mBuilder.setProgress(0, 0, true); } else { // 确定进度的 mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 设置为true就是不确定的那种进度条效果 } mNotificationManager.notify(notifyId, mBuilder.build()); }
public void updateNotificationMessage(String title, String message) { if (title != null) { mNotificationBuilder.setContentTitle(title); } if (message != null) { mNotificationBuilder.setContentText(message); } mNotificationManager.notify(mNotificationId, mNotificationBuilder.build()); }
private void preExecute() { taskIsRunning = true; notification = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(getApplicationContext()); builder.setContentTitle("Searching for new music..."); builder.setContentText("Looking for music files..."); builder.setSmallIcon(R.drawable.ic_launcher); builder.setProgress(100, 0, true); notification.notify(id, builder.build()); }
private static void showPlayServicesNotAvilableNotification() { NotificationCompat.Builder nb = new NotificationCompat.Builder(App.getContext()); NotificationManager nm = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE); nb.setContentTitle(App.getContext().getString(R.string.app_name)) .setSmallIcon(R.drawable.ic_notification) .setContentText("Google Play Services are not available") .setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MIN); nm.notify(Defaults.NOTIFCATION_ID, nb.build()); }
/** 取消下载 */ public void stopDownloadNotify() { if (downloadThread != null) { downloadThread.interrupt(); } downloadThread = null; if (!isCustom) { mBuilder.setContentTitle("下载已取消").setProgress(0, 0, false); mNotificationManager.notify(notifyId, mBuilder.build()); } else { showCustomProgressNotify("下载已取消"); } }