private Notification getForegroundNotification(int allowed, int blocked) { Intent main = new Intent(this, ActivityMain.class); PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT); TypedValue tv = new TypedValue(); getTheme().resolveAttribute(R.attr.colorPrimary, tv, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_security_white_24dp) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.msg_started)) .setContentIntent(pi) .setCategory(Notification.CATEGORY_STATUS) .setVisibility(Notification.VISIBILITY_SECRET) .setPriority(Notification.PRIORITY_MIN) .setColor(tv.data) .setOngoing(true) .setAutoCancel(false); if (allowed > 0 || blocked > 0) { NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder); notification.bigText(getString(R.string.msg_started)); notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked)); return notification.build(); } else return builder.build(); }
public static void showNotActivatedNotification(String packageName, String appName) { Intent intent = new Intent(sContext, WelcomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("fragment", 1); PendingIntent pModulesTab = PendingIntent.getActivity( sContext, PENDING_INTENT_OPEN_MODULES, intent, PendingIntent.FLAG_UPDATE_CURRENT); String title = sContext.getString(R.string.module_is_not_activated_yet); NotificationCompat.Builder builder = new NotificationCompat.Builder(sContext) .setContentTitle(title) .setContentText(appName) .setTicker(title) .setContentIntent(pModulesTab) .setVibrate(new long[] {0}) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_notification) .setColor(sContext.getResources().getColor(R.color.colorPrimary)); if (Build.VERSION.SDK_INT >= 21) builder.setPriority(2); Intent iActivateAndReboot = new Intent(sContext, RebootReceiver.class); iActivateAndReboot.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE, packageName); PendingIntent pActivateAndReboot = PendingIntent.getBroadcast( sContext, PENDING_INTENT_ACTIVATE_MODULE_AND_REBOOT, iActivateAndReboot, PendingIntent.FLAG_UPDATE_CURRENT); Intent iActivate = new Intent(sContext, RebootReceiver.class); iActivate.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE, packageName); iActivate.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE_AND_RETURN, true); PendingIntent pActivate = PendingIntent.getBroadcast( sContext, PENDING_INTENT_ACTIVATE_MODULE, iActivate, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle(title); notiStyle.bigText(sContext.getString(R.string.module_is_not_activated_yet_detailed, appName)); builder.setStyle(notiStyle); // Only show the quick activation button if any module has been // enabled before, // to ensure that the user know the way to disable the module later. if (!ModuleUtil.getInstance().getEnabledModules().isEmpty()) { builder.addAction( R.drawable.ic_menu_refresh, sContext.getString(R.string.activate_and_reboot), pActivateAndReboot); builder.addAction(R.drawable.ic_save, sContext.getString(R.string.activate_only), pActivate); } sNotificationManager.notify( packageName, NOTIFICATION_MODULE_NOT_ACTIVATED_YET, builder.build()); }
/** * Big Text Style Notification * * @return Notification * @see CreateNotification */ private Notification setBigTextStyleNotification() { Bitmap remote_picture = null; // Create the style object with BigTextStyle subclass. NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle("Big Text Expanded"); notiStyle.setSummaryText("Nice big text."); try { remote_picture = BitmapFactory.decodeStream((InputStream) new URL(sample_url).getContent()); } catch (IOException e) { e.printStackTrace(); } // Add the big text to the style. CharSequence bigText = "This is an example of a large string to demo how much " + "text you can show in a 'Big Text Style' notification."; notiStyle.bigText(bigText); // Creates an explicit intent for an ResultActivity to receive. Intent resultIntent = new Intent(this, ResultActivity.class); // This ensures that the back button follows the recommended convention for the back key. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself). stackBuilder.addParentStack(ResultActivity.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); return new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setLargeIcon(remote_picture) .setContentIntent(resultPendingIntent) .addAction(R.drawable.ic_launcher, "One", resultPendingIntent) .addAction(R.drawable.ic_launcher, "Two", resultPendingIntent) .addAction(R.drawable.ic_launcher, "Three", resultPendingIntent) .setContentTitle("Big Text Normal") .setContentText("This is an example of a Big Text Style.") .setStyle(notiStyle) .build(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_steprecording); // 手錶 String fromwear = getIntent().getStringExtra("reply"); if (fromwear == null) { Bundle remoteInput = RemoteInput.getResultsFromIntent(getIntent()); if (remoteInput != null) { fromwear = remoteInput.getCharSequence(Steprecording.EXTRA_VOICE_REPLY).toString(); Log.d("fromwear", fromwear); wearcontent = fromwear.split(" "); TAG_FROM_WEAR = true; } } TextView ss = (TextView) findViewById(R.id.textView6); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); // 取得Bundle /*//是否來過 if(intent.hasExtra("TAG_BACK_TO_RECORDING")){ TAG_BACK_TO_RECORDING = true; Log.d("抓3","backtorecording"); }*/ TAG_CASE_NUMBER = bundle.getString("TAG_CASE_NUMBER"); TAG_STEP_NUMBER = bundle.getString("TAG_STEP_NUMBER"); TAG_STEP_ORDER = bundle.getInt("TAG_STEP_ORDER"); Log.d("StepRecording", TAG_STEP_NUMBER); // TAG_CASE_NUMBER = "15"; // TAG_STEP_NUMBER = "10"; // TAG_STEP_ORDER = 1; ss.setText(Integer.toString(TAG_STEP_ORDER)); // 是否來過 DatabaseHelper mDBACK = DatabaseHelper.getHelper(this); case_recordDao mcase_recordDao = new case_recordDao(); List<case_recordVo> listBACK = null; listBACK = mcase_recordDao.selectRaw( mDBACK, "Case_number=" + TAG_CASE_NUMBER + " and Step_order=" + TAG_STEP_ORDER); if (listBACK.size() != 0) { TAG_BACK_TO_RECORDING = true; } // Hashmap for ListView productsList = new ArrayList<HashMap<String, String>>(); // Loading products in Background Thread // new LoadInput().execute(); // SELECT record_order,record_text FROM step_record WHERE step_number='"+Stepnumber+"'" DatabaseHelper mDatabaseHelper = DatabaseHelper.getHelper(this); mstep_recordDao = new step_recordDao(); List<step_recordVo> list = null; list = mstep_recordDao.selectRaw(mDatabaseHelper, "Step_number =\"" + TAG_STEP_NUMBER + "\""); // Log.d("抓", list.get(0).getRecord_order()); // Log.d("抓2", list.get(0).getRecord_text()); count = list.size(); LinearLayout ly = (LinearLayout) findViewById(R.id.linearlayoutinput); for (int i = 0; i < count; i++) { TextView text1 = new TextView(Steprecording.this); text1.setText(list.get(i).getRecord_text() + "(" + list.get(i).getRecord_unit() + ")"); // 1數字2文字 if (Integer.valueOf(list.get(i).getRecord_type()) == 1) { text1.setText( text1.getText() + "(" + list.get(i).getRecord_min() + "~" + list.get(i).getRecord_max() + ")"); } edit1[i] = new EditText(Steprecording.this.getApplicationContext()); edit1[i].setTextColor(Color.rgb(0, 0, 0)); edit1[i].setOnFocusChangeListener(new MyOnFocusChangeListener()); edit1[i].setSingleLine(true); edit1[i].setBackgroundColor(Color.parseColor("#FDDCB9")); // #FEFBE6黃 if (TAG_FROM_WEAR) { if (i < wearcontent.length) { edit1[i].setText(wearcontent[i]); } } // edit1[0].setText("eee"); ly.addView(text1); ly.addView(edit1[i]); } goright = (ImageButton) findViewById(R.id.imageButton); goleft = (ImageButton) findViewById(R.id.imageButton2); detector = new GestureDetector(new MySimpleOnGestureListener()); ScrollView sv = (ScrollView) findViewById(R.id.scrollView); sv.setOnTouchListener(new MyOnTouchListener()); // LinearLayout llb = (LinearLayout)findViewById(R.id.linearLayoutbackground); // llb.setOnTouchListener(new MyOnTouchListener()); messageList = new ArrayList<>(); // 手錶 if (!TAG_BACK_TO_RECORDING) { String replyLabel = getResources().getString(R.string.reply_label); RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(replyLabel).build(); // Create an intent for the reply action Intent replyIntent = new Intent(this, Steprecording.class); Bundle wb = new Bundle(); wb.putString("TAG_CASE_NUMBER", TAG_CASE_NUMBER); wb.putString("TAG_STEP_NUMBER", TAG_STEP_NUMBER); wb.putInt("TAG_STEP_ORDER", TAG_STEP_ORDER); replyIntent.putExtras(wb); // replyIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action action = new NotificationCompat.Action.Builder( R.drawable.cast_ic_notification_0, getString(R.string.reply_label), replyPendingIntent) .addRemoteInput(remoteInput) .build(); // Create builder for the main notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Step" + Integer.toString(TAG_STEP_ORDER)) .setContentText("紀錄項(請往左滑):"); /* // Create a big text style for the second page NotificationCompat.BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle(); secondPageStyle.setBigContentTitle("Page 2") .bigText("A lot of text..."); // Create second page notification Notification secondPageNotification = new NotificationCompat.Builder(this) .setStyle(secondPageStyle) .build(); */ List extras = new ArrayList(); for (int i = 0; i < count; i++) { NotificationCompat.BigTextStyle extraPageStyle = new NotificationCompat.BigTextStyle(); extraPageStyle .setBigContentTitle("第" + String.valueOf(i) + "項") .bigText(list.get(i).getRecord_text()); Notification extraPageNotification = new NotificationCompat.Builder(this).setStyle(extraPageStyle).build(); extras.add(extraPageNotification); } /* //如果要跳頁 Intent mainIntent = new Intent(this, MyDisplayActivity.class); PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_CANCEL_CURRENT); */ // Extend the notification builder with the second page Notification notification = notificationBuilder .extend(new NotificationCompat.WearableExtender().addPages(extras).addAction(action)) // .addPage(secondPageNotification)) // .addAction(android.R.drawable.ic_media_play, "Speak", mainPendingIntent) .build(); // Issue the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplication()); Random random = new Random(); int notificationId = random.nextInt(9999 - 1000) + 1000; notificationManager.notify(notificationId, notification); /* mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle connectionHint) { Log.d(TAG, "onConnected: " + connectionHint); } @Override public void onConnectionSuspended(int cause) { Log.d(TAG, "onConnectionSuspended: " + cause); } }) .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult result) { Log.d(TAG, "onConnectionFailed: " + result); } }) .addApi(Wearable.API) .build(); mGoogleApiClient.connect(); sendNotification();*/ } }
private void initBigTextStyle(String title, String content) { bigTextStyle = new NotificationCompat.BigTextStyle(); bigTextStyle.setBigContentTitle(title); bigTextStyle.bigText(content); }
private void setNotificationMessage( int notId, Bundle extras, NotificationCompat.Builder mBuilder) { String message = extras.getString(MESSAGE); String style = extras.getString(STYLE, STYLE_TEXT); if (STYLE_INBOX.equals(style)) { setNotification(notId, message); mBuilder.setContentText(fromHtml(message)); ArrayList<String> messageList = messageMap.get(notId); Integer sizeList = messageList.size(); if (sizeList > 1) { String sizeListMessage = sizeList.toString(); String stacking = sizeList + " more"; if (extras.getString(SUMMARY_TEXT) != null) { stacking = extras.getString(SUMMARY_TEXT); stacking = stacking.replace("%n%", sizeListMessage); } NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle() .setBigContentTitle(fromHtml(extras.getString(TITLE))) .setSummaryText(fromHtml(stacking)); for (int i = messageList.size() - 1; i >= 0; i--) { notificationInbox.addLine(fromHtml(messageList.get(i))); } mBuilder.setStyle(notificationInbox); } else { NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); if (message != null) { bigText.bigText(fromHtml(message)); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); mBuilder.setStyle(bigText); } } } else if (STYLE_PICTURE.equals(style)) { setNotification(notId, ""); NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle(); bigPicture.bigPicture(getBitmapFromURL(extras.getString(PICTURE))); bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE))); bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT))); mBuilder.setContentTitle(fromHtml(extras.getString(TITLE))); mBuilder.setContentText(fromHtml(message)); mBuilder.setStyle(bigPicture); } else { setNotification(notId, ""); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); if (message != null) { mBuilder.setContentText(fromHtml(message)); bigText.bigText(fromHtml(message)); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); String summaryText = extras.getString(SUMMARY_TEXT); if (summaryText != null) { bigText.setSummaryText(fromHtml(summaryText)); } mBuilder.setStyle(bigText); } /* else { mBuilder.setContentText("<missing message content>"); } */ } }
private void showNotification( Context context, int notificationId, int smallIconId, String title, String contentText, String bigTitle, String bigContentText, String summaryText, String ticker, Intent intent) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSound(soundUri); if (smallIconId == 0) { builder.setSmallIcon(R.mipmap.ic_launcher); } else { builder.setSmallIcon(smallIconId); } builder.setWhen(System.currentTimeMillis()); // builder.setNumber(10); if (!StringUtils.isEmptyString(ticker)) { builder.setTicker(ticker); } if (StringUtils.isEmptyString(title)) { builder.setContentTitle(PackageUtils.getApplicationName(context)); } else { builder.setContentTitle(title); } builder.setContentText(contentText); builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); builder.setAutoCancel(true); // big title and text if (!StringUtils.isEmptyString(bigTitle) && !StringUtils.isEmptyString(bigContentText)) { NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(builder); if (!StringUtils.isEmptyString(summaryText)) { style.setSummaryText(summaryText); } style.setBigContentTitle(bigTitle); style.bigText(bigContentText); builder.setStyle(style); } if (intent != null) { intent.setFlags( intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity( context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); } notificationManager.notify(notificationId, builder.build()); }
public void postNotification(int id) { if (notificationItems.size() == 0) { return; } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); mBuilder.setContentTitle(notificationTitle); mBuilder.setSmallIcon(R.drawable.ic_stat_icon); mBuilder.setLargeIcon( BitmapFactory.decodeResource( context.getResources(), R.drawable.ic_action_notification_dark)); if (notificationItems.size() > 1) { // inbox style NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(); try { inbox.setBigContentTitle(notificationTitle); } catch (Exception e) { } if (notificationItems.size() <= 5) { for (String s : notificationItems) { inbox.addLine(Html.fromHtml(s)); } } else { for (int i = 0; i < 5; i++) { inbox.addLine(Html.fromHtml(notificationItems.get(i))); } int extra = notificationItems.size() - 5; if (extra > 1) { inbox.setSummaryText("+" + extra + " " + context.getString(R.string.items)); } else { inbox.setSummaryText("+" + extra + " " + context.getString(R.string.item)); } } mBuilder.setStyle(inbox); mBuilder.setContentText(notificationItems.size() + " " + context.getString(R.string.items)); } else { // big text style NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); bigText.bigText(Html.fromHtml(notificationItems.get(0))); bigText.setBigContentTitle(notificationTitle); mBuilder.setStyle(bigText); mBuilder.setContentText(Html.fromHtml(notificationItems.get(0))); } if (!useSecondAccount) { mBuilder.setContentIntent( PendingIntent.getActivity(context, 0, new Intent(context, RedirectToActivity.class), 0)); } if (settings.vibrate) { mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); } if (settings.sound) { try { mBuilder.setSound(Uri.parse(settings.ringtone)); } catch (Exception e) { mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); } } if (settings.led) { mBuilder.setLights(0xFFFFFF, 1000, 1000); } if (settings.wakeScreen) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock wakeLock = pm.newWakeLock( (PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); wakeLock.acquire(5000); } // Pebble notification if (sharedPrefs.getBoolean("pebble_notification", false)) { NotificationUtils.sendAlertToPebble(context, notificationTitle, notificationItems.get(0)); } // Light Flow notification NotificationUtils.sendToLightFlow(context, notificationTitle, notificationItems.get(0)); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(id, mBuilder.build()); }