List of usage examples for android.support.v4.app NotificationManagerCompat from
public static NotificationManagerCompat from(Context context)
From source file:net.kenevans.android.blecardiacmonitor.BCMBleService.java
@Override public void onCreate() { super.onCreate(); // Post a notification the service is running Intent activityIntent = new Intent(this, DeviceMonitorActivity.class); PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.blecardiacmonitor) .setContentTitle(getString(R.string.service_notification_title)) .setContentText(getString(R.string.service_notification_text)).setContentIntent(viewPendingIntent); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); }
From source file:androidavanzato.wearablenotifications.MainActivity.java
/** * Begin to re-post the sample notification(s). *//* w w w . ja v a 2 s . c om*/ private void updateNotifications(boolean cancelExisting) { // Disable messages to skip notification deleted messages during cancel. sendBroadcast(new Intent(NotificationIntentReceiver.ACTION_DISABLE_MESSAGES).setClass(this, NotificationIntentReceiver.class)); if (cancelExisting) { // Cancel all existing notifications to trigger fresh-posting behavior: For example, // switching from HIGH to LOW priority does not cause a reordering in Notification Shade. NotificationManagerCompat.from(this).cancelAll(); postedNotificationCount = 0; // Post the updated notifications on a delay to avoid a cancel+post race condition // with notification manager. mHandler.removeMessages(MSG_POST_NOTIFICATIONS); mHandler.sendEmptyMessageDelayed(MSG_POST_NOTIFICATIONS, POST_NOTIFICATIONS_DELAY_MS); } else { postNotifications(); } }
From source file:com.linute.linute.API.MyGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * * @param data GCM Bundle received.//from w ww .j a v a 2 s.c o m */ private void sendNotification(Bundle data, String action) { Intent intent = buildIntent(data, action); PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT); //Log.d(TAG, data.toString()); String message = data.getString("message"); //int type = gettNotificationType(data.getString("action")); //String name = data.getString("ownerFullName"); boolean isAnon = "1".equals(data.getString("privacy")); String profileImage = null; switch (action) { case "messager": try { JSONObject image = new JSONObject(data.getString("roomProfileImage")); profileImage = image.getString("original"); } catch (JSONException | NullPointerException e) { } break; default: profileImage = data.getString("ownerProfileImage"); profileImage = (isAnon ? Utils.getAnonImageUrl(String.valueOf(profileImage)) : Utils.getImageUrlOfUser(String.valueOf(profileImage))); } Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); ChatRoom chatRoom = (ChatRoom) intent.getParcelableExtra("chatRoom"); String title = chatRoom != null ? chatRoom.getRoomName() : "Tapt"; final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_untitled_4_01).setColor(Color.BLACK).setContentTitle(title) .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent).setStyle(new NotificationCompat.BigTextStyle().bigText(message)); if (profileImage != null) { File image = null; try { image = Glide.with(this).load(profileImage).downloadOnly(256, 256).get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } if (image != null) { /*ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); manager.getMemoryInfo(info);*/ notificationBuilder.setLargeIcon(getCircleBitmap(image)); } } BigInteger notificationId; Object ownerId = data.get("room"); Object eventId = data.get("event"); if (eventId != null) { notificationId = new BigInteger(String.valueOf(eventId), 16); } else if (ownerId != null) { notificationId = new BigInteger(String.valueOf(ownerId), 16); } else { notificationId = BigInteger.ZERO; } final int notifId = notificationId.intValue(); Notification notifications = notificationBuilder.build(); NotificationManagerCompat.from(this).notify(notificationId.intValue(), notifications); }
From source file:com.master.metehan.filtereagle.Receiver.java
public static void notifyNewApplication(int uid, Context context) { if (uid < 0) return;//from www.j a va 2s. com SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); try { // Get application info String name = TextUtils.join(", ", Util.getApplicationNames(uid, context)); // Get application info PackageManager pm = context.getPackageManager(); String[] packages = pm.getPackagesForUid(uid); if (packages == null || packages.length < 1) throw new PackageManager.NameNotFoundException(Integer.toString(uid)); boolean internet = Util.hasInternet(uid, context); // Build notification Intent main = new Intent(context, ActivityMain.class); main.putExtra(ActivityMain.EXTRA_REFRESH, true); main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid)); PendingIntent pi = PendingIntent.getActivity(context, uid, main, PendingIntent.FLAG_UPDATE_CURRENT); Util.setTheme(context); TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_security_white_24dp) .setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.msg_installed, name)).setContentIntent(pi) .setColor(tv.data).setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET); } // Get defaults SharedPreferences prefs_wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE); SharedPreferences prefs_other = context.getSharedPreferences("other", Context.MODE_PRIVATE); boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true)); boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true)); // Build Wi-Fi action Intent riWifi = new Intent(context, ServiceSinkhole.class); riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi"); riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid); riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi); PendingIntent piWifi = PendingIntent.getService(context, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(wifi ? R.drawable.wifi_on : R.drawable.wifi_off, context.getString(wifi ? R.string.title_allow : R.string.title_block), piWifi); // Build mobile action Intent riOther = new Intent(context, ServiceSinkhole.class); riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other"); riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid); riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other); PendingIntent piOther = PendingIntent.getService(context, uid + 10000, riOther, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(other ? R.drawable.other_on : R.drawable.other_off, context.getString(other ? R.string.title_allow : R.string.title_block), piOther); // Show notification if (internet) NotificationManagerCompat.from(context).notify(uid, builder.build()); else { NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder); expanded.bigText(context.getString(R.string.msg_installed, name)); expanded.setSummaryText(context.getString(R.string.title_internet)); NotificationManagerCompat.from(context).notify(uid, expanded.build()); } } catch (PackageManager.NameNotFoundException ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } }
From source file:cx.ring.model.Conference.java
public void showCallNotification(Context ctx) { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(ctx); notificationManager.cancel(notificationId); if (getParticipants().isEmpty()) return;//from ww w.j a va 2 s . c om SipCall call = getParticipants().get(0); CallContact contact = call.getContact(); final Uri call_uri = Uri.withAppendedPath(SipCall.CONTENT_URI, call.getCallId()); PendingIntent goto_intent = PendingIntent.getActivity(ctx, new Random().nextInt(), getViewIntent(ctx), PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder noti = new NotificationCompat.Builder(ctx); if (isOnGoing()) { noti.setContentTitle(ctx.getString(R.string.notif_current_call_title, contact.getDisplayName())) .setContentText(ctx.getText(R.string.notif_current_call)).setContentIntent(goto_intent) .addAction(R.drawable.ic_call_end_white_24dp, ctx.getText(R.string.action_call_hangup), PendingIntent.getService( ctx, new Random().nextInt(), new Intent(LocalService.ACTION_CALL_END) .setClass(ctx, LocalService.class).setData(call_uri), PendingIntent.FLAG_ONE_SHOT)); } else if (isRinging()) { if (isIncoming()) { noti.setContentTitle(ctx.getString(R.string.notif_incoming_call_title, contact.getDisplayName())) .setPriority(NotificationCompat.PRIORITY_MAX) .setContentText(ctx.getText(R.string.notif_incoming_call)).setContentIntent(goto_intent) .setFullScreenIntent(goto_intent, true) .addAction(R.drawable.ic_action_accept, ctx.getText(R.string.action_call_accept), PendingIntent.getService(ctx, new Random().nextInt(), new Intent(LocalService.ACTION_CALL_ACCEPT) .setClass(ctx, LocalService.class).setData(call_uri), PendingIntent.FLAG_ONE_SHOT)) .addAction(R.drawable.ic_call_end_white_24dp, ctx.getText(R.string.action_call_decline), PendingIntent.getService(ctx, new Random().nextInt(), new Intent(LocalService.ACTION_CALL_REFUSE) .setClass(ctx, LocalService.class).setData(call_uri), PendingIntent.FLAG_ONE_SHOT)); } else { noti.setContentTitle(ctx.getString(R.string.notif_outgoing_call_title, contact.getDisplayName())) .setContentText(ctx.getText(R.string.notif_outgoing_call)).setContentIntent(goto_intent) .addAction(R.drawable.ic_call_end_white_24dp, ctx.getText(R.string.action_call_hangup), PendingIntent.getService( ctx, new Random().nextInt(), new Intent(LocalService.ACTION_CALL_END) .setClass(ctx, LocalService.class).setData(call_uri), PendingIntent.FLAG_ONE_SHOT)); } } else { notificationManager.cancel(notificationId); return; } noti.setOngoing(true).setCategory(NotificationCompat.CATEGORY_CALL).setSmallIcon(R.drawable.ic_launcher); if (contact.getPhoto() != null) { Resources res = ctx.getResources(); int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height); int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width); noti.setLargeIcon(Bitmap.createScaledBitmap(contact.getPhoto(), width, height, false)); } notificationManager.notify(notificationId, noti.build()); }
From source file:com.zhengde163.netguard.Receiver.java
public static void notifyNewApplication(int uid, Context context) { if (uid < 0) return;/*from w w w.j ava 2s.c o m*/ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); try { // Get application info String name = TextUtils.join(", ", Util.getApplicationNames(uid, context)); // Get application info PackageManager pm = context.getPackageManager(); String[] packages = pm.getPackagesForUid(uid); if (packages == null || packages.length < 1) throw new PackageManager.NameNotFoundException(Integer.toString(uid)); boolean internet = Util.hasInternet(uid, context); // Build notification Intent main = new Intent(context, ActivityMain.class); main.putExtra(ActivityMain.EXTRA_REFRESH, true); main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid)); PendingIntent pi = PendingIntent.getActivity(context, uid, main, PendingIntent.FLAG_UPDATE_CURRENT); // Util.setTheme(context); context.setTheme(R.style.AppThemeBlue); TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_security_white_24dp) .setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.msg_installed, name)).setContentIntent(pi) .setColor(tv.data).setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET); } // Get defaults SharedPreferences prefs_wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE); SharedPreferences prefs_other = context.getSharedPreferences("other", Context.MODE_PRIVATE); boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true)); boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true)); // Build Wi-Fi action Intent riWifi = new Intent(context, ServiceSinkhole.class); riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi"); riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid); riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi); PendingIntent piWifi = PendingIntent.getService(context, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(wifi ? R.drawable.wifi_on : R.drawable.wifi_off, context.getString(wifi ? R.string.title_allow : R.string.title_block), piWifi); // Build mobile action Intent riOther = new Intent(context, ServiceSinkhole.class); riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other"); riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid); riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other); PendingIntent piOther = PendingIntent.getService(context, uid + 10000, riOther, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(other ? R.drawable.other_on : R.drawable.other_off, context.getString(other ? R.string.title_allow : R.string.title_block), piOther); // Show notification if (internet) NotificationManagerCompat.from(context).notify(uid, builder.build()); else { NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder); expanded.bigText(context.getString(R.string.msg_installed, name)); expanded.setSummaryText(context.getString(R.string.title_internet)); NotificationManagerCompat.from(context).notify(uid, expanded.build()); } } catch (PackageManager.NameNotFoundException ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } }
From source file:org.dutchaug.android.wearable.notifications.PhoneActivity.java
/** * Builds a local-only notification for the handset. This is achieved by using * <code>setLocalOnly(true)</code>. If <code>withDismissal</code> is set to <code>true</code>, a * {@link android.app.PendingIntent} will be added to handle the dismissal of notification to * be able to remove the mirrored notification on the wearable. *///from w ww . ja va2s. com private void buildLocalOnlyNotification(String title, String content, int notificationId, boolean withDismissal) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle(title).setContentText(content).setSmallIcon(R.drawable.ic_notification) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification_big)) .setLocalOnly(true); // Build intent for notification content Intent viewIntent = new Intent(this, ActionActivity.class); viewIntent.putExtra(ActionActivity.EXTRA_NOTIFICATION_ID, notificationId); PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(viewPendingIntent); // Build intent for action notification content Intent actionIntent = new Intent(this, ActionActivity.class); actionIntent.setAction(ActionActivity.ACTION_DO_MAGIC); actionIntent.putExtra(ActionActivity.EXTRA_NOTIFICATION_ID, notificationId); PendingIntent actionPendingIntent = PendingIntent.getActivity(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(R.drawable.ic_action, getString(R.string.action_title), actionPendingIntent); if (withDismissal) { // Send an intent when the notification is deleted Intent dismissIntent = new Intent(Constants.ACTION_DISMISS); dismissIntent.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.BOTH_ID); PendingIntent pendingIntent = PendingIntent.getService(this, 0, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setDeleteIntent(pendingIntent); } // Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // Build the notification and issues it with notification manager notificationManager.notify(notificationId, builder.build()); }
From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java
private void initialize() { mNotificationManager = NotificationManagerCompat.from(this); mMdnsUrlDiscoverer = new MdnsUrlDiscoverer(this, UriBeaconDiscoveryService.this); mHandler = new Handler(); initializeScreenStateBroadcastReceiver(); }
From source file:net.kenevans.android.blecardiacmonitor.BCMBleService.java
@Override public void onDestroy() { // Cancel the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.cancel(NOTIFICATION_ID); super.onDestroy(); }
From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java
public static void updateAlarmGroupMissedNotification(Context context) { if (!Utils.isNOrLater()) { return;// w w w. j av a 2 s.com } NotificationManagerCompat nm = NotificationManagerCompat.from(context); if (getActiveNotificationsCount(context, MISSED_GROUP_KEY) == 0) { nm.cancel(ALARM_GROUP_MISSED_NOTIFICATION_ID); return; } NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(context).setShowWhen(false) .setColor(ContextCompat.getColor(context, R.color.default_background)) .setSmallIcon(R.drawable.stat_notify_alarm).setGroup(MISSED_GROUP_KEY).setGroupSummary(true) .setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_ALARM) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setLocalOnly(true); nm.notify(ALARM_GROUP_MISSED_NOTIFICATION_ID, summaryNotification.build()); }