@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void triggerNotification(String info, String info2, Context context, Intent intent) { NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_directions_bus_black_48dp) .setContentTitle(context.getResources().getString(R.string.app_name)) .setAutoCancel(true) .setStyle(new NotificationCompat.BigTextStyle().bigText(info2)) .setContentText(info) .setVibrate(null) .setSound(null) .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0)); // Notifications if (Repository.getInstance().getRingtoneNotification().equals(true)) { // Ton Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mBuilder.setSound(alarmSound); } if (Repository.getInstance().getVibrationNotification().equals(true)) { // Vibration mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000}); } if (Repository.getInstance().getLedNotification().equals(true)) { // LED mBuilder.setLights(ContextCompat.getColor(context, R.color.ledColour), 1000, 1000); } // Creates an explicit intent for an Activity Intent resultIntent = new Intent(context, MainActivity.class); // 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 // application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(MainActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(418, mBuilder.build()); }
public static void sendBroadcastNotification( Context context, Intent intent, String contentTitle, String contentText, int id) { NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setDefaults(Notification.DEFAULT_ALL); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle(contentTitle); builder.setContentText(contentText); builder.setContentIntent(pendingIntent); builder.setVibrate(new long[] {200}); builder.setAutoCancel(true); builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); Notification n = builder.build(); manager.notify(id, n); }
private void notifyInToolBar() { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); builder.setTicker("天气信息已经更新!"); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setWhen(System.currentTimeMillis()); builder.setAutoCancel(true); // 设置自定义RemoteView RemoteViews view = new RemoteViews(getPackageName(), R.layout.remote_view); builder.setContent(view); PendingIntent pi = PendingIntent.getActivity( MainActivity.this, 1, new Intent(MainActivity.this, MainActivity.class), 0); builder.setContentIntent(pi); builder.setVibrate(new long[] {1000, 1000, 1000, 1000}); builder.setLights(Color.RED, 0, 1); builder.setOngoing(true); manager.notify(2, builder.build()); }