List of usage examples for android.app Notification DEFAULT_SOUND
int DEFAULT_SOUND
To view the source code for android.app Notification DEFAULT_SOUND.
Click Source Link
From source file:com.wondertoys.pokevalue.utils.AutoUpdateApk.java
protected void raise_notification() { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nm = (NotificationManager) context.getSystemService(ns); String update_file = preferences.getString(UPDATE_FILE, ""); if (update_file.length() > 0) { setChanged();/*from www . j ava 2 s . c o m*/ notifyObservers(AUTOUPDATE_HAVE_UPDATE); // raise the notification CharSequence contentTitle = appName + " update available"; CharSequence contentText = "Select to install"; Intent notificationIntent = new Intent(Intent.ACTION_VIEW); notificationIntent.setDataAndType(Uri.parse( "file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + update_file), ANDROID_PACKAGE); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); builder.setSmallIcon(appIcon); builder.setTicker(appName + " update"); builder.setContentTitle(contentTitle); builder.setContentText(contentText); builder.setContentIntent(contentIntent); builder.setWhen(System.currentTimeMillis()); builder.setAutoCancel(true); builder.setOngoing(true); nm.notify(NOTIFICATION_ID, builder.build()); } else { //nm.cancel( NOTIFICATION_ID ); // tried this, but it just doesn't do the trick =( nm.cancelAll(); } }
From source file:com.google.appinventor.components.runtime.ProbeBase.java
@SimpleFunction(description = "Create a notication with message to wake up " + "another activity when tap on the notification") public void CreateNotification(String title, String text, boolean enabledSound, boolean enabledVibrate, String packageName, String className, String extraKey, String extraVal) throws ClassNotFoundException { Intent activityToLaunch = new Intent(Intent.ACTION_MAIN); Log.i(TAG, "packageName: " + packageName); Log.i(TAG, "className: " + className); // for local AI instance, all classes are under the package // "appinventor.ai_test" // but for those runs on Google AppSpot(AppEngine), the package name will be // "appinventor.ai_GoogleAccountUserName" // e.g. pakageName = appinventor.ai_HomerSimpson.HelloPurr // && className = appinventor.ai_HomerSimpson.HelloPurr.Screen1 ComponentName component = new ComponentName(packageName, className); activityToLaunch.setComponent(component); activityToLaunch.putExtra(extraKey, extraVal); Log.i(TAG, "we found the class for intent to send into notificaiton"); activityToLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); mContentIntent = PendingIntent.getActivity(mainUIThreadActivity, 0, activityToLaunch, 0); Long currentTimeMillis = System.currentTimeMillis(); notification = new Notification(R.drawable.stat_notify_chat, "Activate Notification!", currentTimeMillis); Log.i(TAG, "After creating notification"); notification.contentIntent = mContentIntent; notification.flags = Notification.FLAG_AUTO_CANCEL; // reset the notification notification.defaults = 0;//from w ww . j a v a 2s .c om if (enabledSound) notification.defaults |= Notification.DEFAULT_SOUND; if (enabledVibrate) notification.defaults |= Notification.DEFAULT_VIBRATE; notification.setLatestEventInfo(mainUIThreadActivity, (CharSequence) title, (CharSequence) text, mContentIntent); Log.i(TAG, "after updated notification contents"); mNM.notify(PROBE_NOTIFICATION_ID, notification); Log.i(TAG, "notified"); }
From source file:com.nbplus.vbroadlauncher.BroadcastPushReceiver.java
private void showNotification(Context context, int notificationId, 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);/*from ww w. j a va2 s . c o m*/ builder.setSmallIcon(R.drawable.ic_notification_noti); 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) { PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); } notificationManager.notify(notificationId, builder.build()); }
From source file:nl.vanvianen.android.gcm.GCMIntentService.java
@Override @SuppressWarnings("unchecked") protected void onMessage(Context context, Intent intent) { Log.d(LCAT, "Push notification received"); boolean isTopic = false; HashMap<String, Object> data = new HashMap<String, Object>(); for (String key : intent.getExtras().keySet()) { Object value = intent.getExtras().get(key); Log.d(LCAT, "Message key: \"" + key + "\" value: \"" + value + "\""); if (key.equals("from") && value instanceof String && ((String) value).startsWith("/topics/")) { isTopic = true;//from ww w .j av a 2 s . c o m } String eventKey = key.startsWith("data.") ? key.substring(5) : key; data.put(eventKey, intent.getExtras().get(key)); if (value instanceof String && ((String) value).startsWith("{")) { Log.d(LCAT, "Parsing JSON string..."); try { JSONObject json = new JSONObject((String) value); Iterator<String> keys = json.keys(); while (keys.hasNext()) { String jKey = keys.next(); String jValue = json.getString(jKey); Log.d(LCAT, "JSON key: \"" + jKey + "\" value: \"" + jValue + "\""); data.put(jKey, jValue); } } catch (JSONException ex) { Log.d(LCAT, "JSON error: " + ex.getMessage()); } } } /* Store data to be retrieved when resuming app as a JSON object, serialized as a String, otherwise * Ti.App.Properties.getString(GCMModule.LAST_DATA) doesn't work. */ JSONObject json = new JSONObject(data); TiApplication.getInstance().getAppProperties().setString(GCMModule.LAST_DATA, json.toString()); /* Get settings from notification object */ int smallIcon = 0; int largeIcon = 0; String sound = null; boolean vibrate = false; boolean insistent = false; String group = null; boolean localOnly = true; int priority = 0; boolean bigText = false; int notificationId = 1; Integer ledOn = null; Integer ledOff = null; String titleKey = DEFAULT_TITLE_KEY; String messageKey = DEFAULT_MESSAGE_KEY; String tickerKey = DEFAULT_TICKER_KEY; String title = null; String message = null; String ticker = null; boolean backgroundOnly = false; Map<String, Object> notificationSettings = new Gson().fromJson( TiApplication.getInstance().getAppProperties().getString(GCMModule.NOTIFICATION_SETTINGS, null), Map.class); if (notificationSettings != null) { if (notificationSettings.get("smallIcon") instanceof String) { smallIcon = getResource("drawable", (String) notificationSettings.get("smallIcon")); } else { Log.e(LCAT, "Invalid setting smallIcon, should be String"); } if (notificationSettings.get("largeIcon") instanceof String) { largeIcon = getResource("drawable", (String) notificationSettings.get("largeIcon")); } else { Log.e(LCAT, "Invalid setting largeIcon, should be String"); } if (notificationSettings.get("sound") != null) { if (notificationSettings.get("sound") instanceof String) { sound = (String) notificationSettings.get("sound"); } else { Log.e(LCAT, "Invalid setting sound, should be String"); } } if (notificationSettings.get("vibrate") != null) { if (notificationSettings.get("vibrate") instanceof Boolean) { vibrate = (Boolean) notificationSettings.get("vibrate"); } else { Log.e(LCAT, "Invalid setting vibrate, should be Boolean"); } } if (notificationSettings.get("insistent") != null) { if (notificationSettings.get("insistent") instanceof Boolean) { insistent = (Boolean) notificationSettings.get("insistent"); } else { Log.e(LCAT, "Invalid setting insistent, should be Boolean"); } } if (notificationSettings.get("group") != null) { if (notificationSettings.get("group") instanceof String) { group = (String) notificationSettings.get("group"); } else { Log.e(LCAT, "Invalid setting group, should be String"); } } if (notificationSettings.get("localOnly") != null) { if (notificationSettings.get("localOnly") instanceof Boolean) { localOnly = (Boolean) notificationSettings.get("localOnly"); } else { Log.e(LCAT, "Invalid setting localOnly, should be Boolean"); } } if (notificationSettings.get("priority") != null) { if (notificationSettings.get("priority") instanceof Integer) { priority = (Integer) notificationSettings.get("priority"); } else if (notificationSettings.get("priority") instanceof Double) { priority = ((Double) notificationSettings.get("priority")).intValue(); } else { Log.e(LCAT, "Invalid setting priority, should be an integer, between PRIORITY_MIN (" + NotificationCompat.PRIORITY_MIN + ") and PRIORITY_MAX (" + NotificationCompat.PRIORITY_MAX + ")"); } } if (notificationSettings.get("bigText") != null) { if (notificationSettings.get("bigText") instanceof Boolean) { bigText = (Boolean) notificationSettings.get("bigText"); } else { Log.e(LCAT, "Invalid setting bigText, should be Boolean"); } } if (notificationSettings.get("titleKey") != null) { if (notificationSettings.get("titleKey") instanceof String) { titleKey = (String) notificationSettings.get("titleKey"); } else { Log.e(LCAT, "Invalid setting titleKey, should be String"); } } if (notificationSettings.get("messageKey") != null) { if (notificationSettings.get("messageKey") instanceof String) { messageKey = (String) notificationSettings.get("messageKey"); } else { Log.e(LCAT, "Invalid setting messageKey, should be String"); } } if (notificationSettings.get("tickerKey") != null) { if (notificationSettings.get("tickerKey") instanceof String) { tickerKey = (String) notificationSettings.get("tickerKey"); } else { Log.e(LCAT, "Invalid setting tickerKey, should be String"); } } if (notificationSettings.get("title") != null) { if (notificationSettings.get("title") instanceof String) { title = (String) notificationSettings.get("title"); } else { Log.e(LCAT, "Invalid setting title, should be String"); } } if (notificationSettings.get("message") != null) { if (notificationSettings.get("message") instanceof String) { message = (String) notificationSettings.get("message"); } else { Log.e(LCAT, "Invalid setting message, should be String"); } } if (notificationSettings.get("ticker") != null) { if (notificationSettings.get("ticker") instanceof String) { ticker = (String) notificationSettings.get("ticker"); } else { Log.e(LCAT, "Invalid setting ticker, should be String"); } } if (notificationSettings.get("ledOn") != null) { if (notificationSettings.get("ledOn") instanceof Integer) { ledOn = (Integer) notificationSettings.get("ledOn"); if (ledOn < 0) { Log.e(LCAT, "Invalid setting ledOn, should be positive"); ledOn = null; } } else { Log.e(LCAT, "Invalid setting ledOn, should be Integer"); } } if (notificationSettings.get("ledOff") != null) { if (notificationSettings.get("ledOff") instanceof Integer) { ledOff = (Integer) notificationSettings.get("ledOff"); if (ledOff < 0) { Log.e(LCAT, "Invalid setting ledOff, should be positive"); ledOff = null; } } else { Log.e(LCAT, "Invalid setting ledOff, should be Integer"); } } if (notificationSettings.get("backgroundOnly") != null) { if (notificationSettings.get("backgroundOnly") instanceof Boolean) { backgroundOnly = (Boolean) notificationSettings.get("backgroundOnly"); } else { Log.e(LCAT, "Invalid setting backgroundOnly, should be Boolean"); } } if (notificationSettings.get("notificationId") != null) { if (notificationSettings.get("notificationId") instanceof Integer) { notificationId = (Integer) notificationSettings.get("notificationId"); } else { Log.e(LCAT, "Invalid setting notificationId, should be Integer"); } } } else { Log.d(LCAT, "No notification settings found"); } /* If icon not found, default to appicon */ if (smallIcon == 0) { smallIcon = getResource("drawable", "appicon"); } /* If large icon not found, default to icon */ if (largeIcon == 0) { largeIcon = smallIcon; } /* Create intent to (re)start the app's root activity */ String pkg = TiApplication.getInstance().getApplicationContext().getPackageName(); Intent launcherIntent = TiApplication.getInstance().getApplicationContext().getPackageManager() .getLaunchIntentForPackage(pkg); launcherIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); /* Grab notification content from data according to provided keys if not already set */ if (title == null && titleKey != null) { title = (String) data.get(titleKey); } if (message == null && messageKey != null) { message = (String) data.get(messageKey); } if (ticker == null && tickerKey != null) { ticker = (String) data.get(tickerKey); } Log.i(LCAT, "Title: " + title); Log.i(LCAT, "Message: " + message); Log.i(LCAT, "Ticker: " + ticker); /* Check for app state */ if (GCMModule.getInstance() != null) { /* Send data to app */ if (isTopic) { GCMModule.getInstance().sendTopicMessage(data); } else { GCMModule.getInstance().sendMessage(data); } /* Do not create notification if backgroundOnly and app is in foreground */ if (backgroundOnly && GCMModule.getInstance().isInForeground()) { Log.d(LCAT, "Notification received in foreground, no need for notification."); return; } } if (message == null) { Log.d(LCAT, "Message received but no 'message' specified in push notification payload, so will make this silent"); } else { Log.d(LCAT, "Creating notification..."); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), largeIcon); if (bitmap == null) { Log.d(LCAT, "No large icon found"); } NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentTitle(title) .setContentText(message).setTicker(ticker) .setContentIntent( PendingIntent.getActivity(this, 0, launcherIntent, PendingIntent.FLAG_ONE_SHOT)) .setSmallIcon(smallIcon).setLargeIcon(bitmap); /* Name of group to group similar notifications together, can also be set in the push notification payload */ if (data.get("group") != null) { group = (String) data.get("group"); } if (group != null) { builder.setGroup(group); } Log.i(LCAT, "Group: " + group); /* Whether notification should be for this device only or bridged to other devices, can also be set in the push notification payload */ if (data.get("localOnly") != null) { localOnly = Boolean.valueOf((String) data.get("localOnly")); } builder.setLocalOnly(localOnly); Log.i(LCAT, "LocalOnly: " + localOnly); /* Specify notification priority, can also be set in the push notification payload */ if (data.get("priority") != null) { priority = Integer.parseInt((String) data.get("priority")); } if (priority >= NotificationCompat.PRIORITY_MIN && priority <= NotificationCompat.PRIORITY_MAX) { builder.setPriority(priority); Log.i(LCAT, "Priority: " + priority); } else { Log.e(LCAT, "Ignored invalid priority " + priority); } /* Specify whether bigtext should be used, can also be set in the push notification payload */ if (data.get("bigText") != null) { bigText = Boolean.valueOf((String) data.get("bigText")); } if (bigText) { builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); } Log.i(LCAT, "bigText: " + bigText); Notification notification = builder.build(); /* Sound, can also be set in the push notification payload */ if (data.get("sound") != null) { Log.d(LCAT, "Sound specified in notification"); sound = (String) data.get("sound"); } if ("default".equals(sound)) { Log.i(LCAT, "Sound: default sound"); notification.defaults |= Notification.DEFAULT_SOUND; } else if (sound != null) { Log.i(LCAT, "Sound " + sound); notification.sound = Uri.parse("android.resource://" + pkg + "/" + getResource("raw", sound)); } /* Vibrate, can also be set in the push notification payload */ if (data.get("vibrate") != null) { vibrate = Boolean.valueOf((String) data.get("vibrate")); } if (vibrate) { notification.defaults |= Notification.DEFAULT_VIBRATE; } Log.i(LCAT, "Vibrate: " + vibrate); /* Insistent, can also be set in the push notification payload */ if ("true".equals(data.get("insistent"))) { insistent = true; } if (insistent) { notification.flags |= Notification.FLAG_INSISTENT; } Log.i(LCAT, "Insistent: " + insistent); /* notificationId, set in push payload to specify multiple notifications should be shown. If not specified, subsequent notifications "override / overwrite" the older ones */ if (data.get("notificationId") != null) { if (data.get("notificationId") instanceof Integer) { notificationId = (Integer) data.get("notificationId"); } else if (data.get("notificationId") instanceof String) { try { notificationId = Integer.parseInt((String) data.get("notificationId")); } catch (NumberFormatException ex) { Log.e(LCAT, "Invalid setting notificationId, should be Integer"); } } else { Log.e(LCAT, "Invalid setting notificationId, should be Integer"); } } Log.i(LCAT, "Notification ID: " + notificationId); /* Specify LED flashing */ if (ledOn != null || ledOff != null) { notification.flags |= Notification.FLAG_SHOW_LIGHTS; if (ledOn != null) { notification.ledOnMS = ledOn; } if (ledOff != null) { notification.ledOffMS = ledOff; } } else { notification.defaults |= Notification.DEFAULT_LIGHTS; } notification.flags |= Notification.FLAG_AUTO_CANCEL; ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification); } }
From source file:com.ubergeek42.WeechatAndroid.service.RelayService.java
@Override public void onHighlight(String bufferName, String message) { Intent i = new Intent(this, WeechatActivity.class); i.putExtra("buffer", bufferName); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_notification) .setContentTitle(getString(R.string.app_version)).setContentText(message).setTicker(message) .setWhen(System.currentTimeMillis()); Notification notification = builder.getNotification(); notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; // Default notification sound if enabled if (prefs.getBoolean("notification_sounds", false)) { notification.defaults |= Notification.DEFAULT_SOUND; }/*from w w w . j a v a2 s .c o m*/ notificationManger.notify(NOTIFICATION_ID, notification); }
From source file:net.wespot.pim.view.InqCommunicateFragment.java
private void createNotification(MessageLocalObject messageLocalObject, Long runId) { if (!runIdList.contains(runId)) { runIdList.add(runId);/* w w w . j ava 2 s . c o m*/ runIdList_str.add(runId.toString()); } numMessages = 0; mBuilder = null; mNotificationStyle = null; mNotificationManager = null; mNotificationManager = (NotificationManager) ARL.getContext() .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationStyle = new NotificationCompat.InboxStyle(); mBuilder = new NotificationCompat.Builder(ARL.getContext()).setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true).setSortKey("0") .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setStyle(mNotificationStyle); InquiryLocalObject inquiryLocalObject = DaoConfiguration.getInstance().getInquiryLocalObjectDao() .queryBuilder().where(InquiryLocalObjectDao.Properties.RunId.eq(runId)).list().get(0); // 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(ARL.getContext()); Intent resultIntent; if (runIdList.size() > 1) { resultIntent = new Intent(ARL.getContext(), PimInquiriesFragment.class); resultIntent.putStringArrayListExtra(INQUIRIES_ID, (ArrayList<String>) runIdList_str); Intent parent = new Intent(ARL.getContext(), MainActivity.class); Intent parent1 = new Intent(ARL.getContext(), SplashActivity.class); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addNextIntentWithParentStack(parent1); stackBuilder.addNextIntentWithParentStack(parent); } else { resultIntent = new Intent(ARL.getContext(), InquiryPhasesActivity.class); resultIntent.putExtra(INQUIRY_ID, inquiryLocalObject.getId()); Intent parent = new Intent(ARL.getContext(), PimInquiriesFragment.class); Intent parent1 = new Intent(ARL.getContext(), MainActivity.class); Intent parent2 = new Intent(ARL.getContext(), SplashActivity.class); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addNextIntentWithParentStack(parent2); stackBuilder.addNextIntentWithParentStack(parent1); stackBuilder.addNextIntentWithParentStack(parent); } // 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); mBuilder.setContentIntent(resultPendingIntent); if (runIdList.size() == 1) { // More than 1 inquiries have messages mBuilder.setContentTitle(inquiryLocalObject.getTitle()); for (MessageLocalObject me : notications_queue_messages) { mNotificationStyle.addLine(getNameUser(me.getAuthor()) + ": " + me.getBody()).setSummaryText( ++numMessages != 1 ? numMessages + " new messages" : numMessages + " new message"); } } if (runIdList.size() > 1) { // More than 1 inquiries have messages for (MessageLocalObject me : notications_queue_messages) { mBuilder.setContentTitle("Personal Inquiry Manager"); InquiryLocalObject a = DaoConfiguration.getInstance().getInquiryLocalObjectDao().queryBuilder() .where(InquiryLocalObjectDao.Properties.RunId.eq(me.getRunId())).list().get(0); mNotificationStyle.addLine(getNameUser(me.getAuthor()) + " @ " + a.getTitle() + ": " + me.getBody()) .setSummaryText(++numMessages != 1 ? numMessages + " new messages from " + runIdList.size() + " conversations" + " " : numMessages + " new message from " + runIdList.size() + " conversations"); } } mNotificationManager.notify(Integer.parseInt(NUMBER), mBuilder.build()); }
From source file:fr.paug.droidcon.service.SessionAlarmService.java
private void notifySession(final long sessionStart, final long alarmOffset) { long currentTime = UIUtils.getCurrentTime(this); final long intervalEnd = sessionStart + MILLI_TEN_MINUTES; LOGD(TAG, "Considering notifying for time interval."); LOGD(TAG, " Interval start: " + sessionStart + "=" + (new Date(sessionStart)).toString()); LOGD(TAG, " Interval end: " + intervalEnd + "=" + (new Date(intervalEnd)).toString()); LOGD(TAG, " Current time is: " + currentTime + "=" + (new Date(currentTime)).toString()); if (sessionStart < currentTime) { LOGD(TAG, "Skipping session notification (too late -- time interval already started)"); return;// w w w . j av a 2 s . c o m } if (!PrefUtils.shouldShowSessionReminders(this)) { // skip if disabled in settings LOGD(TAG, "Skipping session notification for sessions. Disabled in settings."); return; } // Avoid repeated notifications. if (alarmOffset == UNDEFINED_ALARM_OFFSET && UIUtils.isNotificationFiredForBlock(this, ScheduleContract.Blocks.generateBlockId(sessionStart, intervalEnd))) { LOGD(TAG, "Skipping session notification (already notified)"); return; } final ContentResolver cr = getContentResolver(); LOGD(TAG, "Looking for sessions in interval " + sessionStart + " - " + intervalEnd); Cursor c = cr.query(ScheduleContract.Sessions.CONTENT_MY_SCHEDULE_URI, SessionDetailQuery.PROJECTION, ScheduleContract.Sessions.STARTING_AT_TIME_INTERVAL_SELECTION, ScheduleContract.Sessions.buildAtTimeIntervalArgs(sessionStart, intervalEnd), null); int starredCount = c.getCount(); LOGD(TAG, "# starred sessions in that interval: " + c.getCount()); String singleSessionId = null; String singleSessionRoomId = null; ArrayList<String> starredSessionTitles = new ArrayList<String>(); while (c.moveToNext()) { singleSessionId = c.getString(SessionDetailQuery.SESSION_ID); singleSessionRoomId = c.getString(SessionDetailQuery.ROOM_ID); starredSessionTitles.add(c.getString(SessionDetailQuery.SESSION_TITLE)); LOGD(TAG, "-> Title: " + c.getString(SessionDetailQuery.SESSION_TITLE)); } if (starredCount < 1) { return; } // Generates the pending intent which gets fired when the user taps on the notification. // NOTE: Use TaskStackBuilder to comply with Android's design guidelines // related to navigation from notifications. Intent baseIntent = new Intent(this, MyScheduleActivity.class); baseIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); TaskStackBuilder taskBuilder = TaskStackBuilder.create(this).addNextIntent(baseIntent); // For a single session, tapping the notification should open the session details (b/15350787) if (starredCount == 1) { taskBuilder.addNextIntent( new Intent(Intent.ACTION_VIEW, ScheduleContract.Sessions.buildSessionUri(singleSessionId))); } PendingIntent pi = taskBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); final Resources res = getResources(); String contentText; int minutesLeft = (int) (sessionStart - currentTime + 59000) / 60000; if (minutesLeft < 1) { minutesLeft = 1; } if (starredCount == 1) { contentText = res.getString(R.string.session_notification_text_1, minutesLeft); } else { contentText = res.getQuantityString(R.plurals.session_notification_text, starredCount - 1, minutesLeft, starredCount - 1); } NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this) .setContentTitle(starredSessionTitles.get(0)).setContentText(contentText) //.setColor(getResources().getColor(R.color.theme_primary)) // Note: setColor() is available in the support lib v21+. // We commented it out because we want the source to compile // against support lib v20. If you are using support lib // v21 or above on Android L, uncomment this line. .setTicker(res.getQuantityString(R.plurals.session_notification_ticker, starredCount, starredCount)) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setLights(SessionAlarmService.NOTIFICATION_ARGB_COLOR, SessionAlarmService.NOTIFICATION_LED_ON_MS, SessionAlarmService.NOTIFICATION_LED_OFF_MS) .setSmallIcon(R.drawable.ic_stat_notification).setContentIntent(pi) .setPriority(Notification.PRIORITY_MAX).setAutoCancel(true); if (minutesLeft > 5) { notifBuilder.addAction(R.drawable.ic_alarm_holo_dark, String.format(res.getString(R.string.snooze_x_min), 5), createSnoozeIntent(sessionStart, intervalEnd, 5)); } String bigContentTitle; if (starredCount == 1 && starredSessionTitles.size() > 0) { bigContentTitle = starredSessionTitles.get(0); } else { bigContentTitle = res.getQuantityString(R.plurals.session_notification_title, starredCount, minutesLeft, starredCount); } NotificationCompat.InboxStyle richNotification = new NotificationCompat.InboxStyle(notifBuilder) .setBigContentTitle(bigContentTitle); // Adds starred sessions starting at this time block to the notification. for (int i = 0; i < starredCount; i++) { richNotification.addLine(starredSessionTitles.get(i)); } NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); LOGD(TAG, "Now showing notification."); nm.notify(NOTIFICATION_ID, richNotification.build()); }
From source file:br.com.arlsoft.pushclient.PushClientModule.java
public static void sendNotification(Bundle extras) { if (extras == null || extras.isEmpty()) return;//from w w w . ja v a 2s . c o m TiApplication appContext = TiApplication.getInstance(); int appIconId = appContext.getResources().getIdentifier("appicon", "drawable", appContext.getPackageName()); String appName = appContext.getAppInfo().getName(); Bundle extrasRoot = extras; int badgeCount = -1; int notificationId = 0; String notificationTitle = null; String notificationText = null; String notificationTicker = null; Uri notificationSound = null; int notificationDefaults = 0; // TEXT if (extras.containsKey("text")) { notificationText = extras.getString("text"); } else if (extras.containsKey("alert")) { notificationText = extras.getString("alert"); } else if (extras.containsKey("message")) { notificationText = extras.getString("message"); } else if (extras.containsKey("data")) { try { JSONObject reader = new JSONObject(extras.getString("data")); Bundle newExtras = new Bundle(); for (int i = 0; i < reader.names().length(); i++) { String key = reader.names().getString(i); newExtras.putString(key, reader.getString(key)); } if (newExtras.containsKey("text")) { notificationText = newExtras.getString("text"); extras = newExtras; } else if (newExtras.containsKey("alert")) { notificationText = newExtras.getString("alert"); extras = newExtras; } else if (newExtras.containsKey("message")) { notificationText = newExtras.getString("message"); extras = newExtras; } } catch (JSONException e) { String text = extras.getString("data"); if (text != null) { notificationText = text; } } } else if (extras.containsKey("msg")) { try { JSONObject reader = new JSONObject(extras.getString("msg")); Bundle newExtras = new Bundle(); for (int i = 0; i < reader.names().length(); i++) { String key = reader.names().getString(i); newExtras.putString(key, reader.getString(key)); } if (newExtras.containsKey("text")) { notificationText = newExtras.getString("text"); extras = newExtras; } else if (newExtras.containsKey("alert")) { notificationText = newExtras.getString("alert"); extras = newExtras; } else if (newExtras.containsKey("message")) { notificationText = newExtras.getString("message"); extras = newExtras; } } catch (JSONException e) { String text = extras.getString("msg"); if (text != null) { notificationText = text; } } } else if (extras.containsKey("default")) { try { JSONObject reader = new JSONObject(extras.getString("default")); Bundle newExtras = new Bundle(); for (int i = 0; i < reader.names().length(); i++) { String key = reader.names().getString(i); newExtras.putString(key, reader.getString(key)); } if (newExtras.containsKey("text")) { notificationText = newExtras.getString("text"); extras = newExtras; } else if (newExtras.containsKey("alert")) { notificationText = newExtras.getString("alert"); extras = newExtras; } else if (newExtras.containsKey("message")) { notificationText = newExtras.getString("message"); extras = newExtras; } } catch (JSONException e) { String text = extras.getString("default"); if (text != null) { notificationText = text; } } } else if (extras.containsKey("payload")) { try { JSONObject reader = new JSONObject(extras.getString("payload")); Bundle newExtras = new Bundle(); for (int i = 0; i < reader.names().length(); i++) { String key = reader.names().getString(i); newExtras.putString(key, reader.getString(key)); } if (newExtras.containsKey("text")) { notificationText = newExtras.getString("text"); extras = newExtras; } else if (newExtras.containsKey("alert")) { notificationText = newExtras.getString("alert"); extras = newExtras; } else if (newExtras.containsKey("message")) { notificationText = newExtras.getString("message"); extras = newExtras; } } catch (JSONException e) { String text = extras.getString("payload"); if (text != null) { notificationText = text; } } } // TITLE if (extras.containsKey("title")) { notificationTitle = extras.getString("title"); } else { notificationTitle = appName; } // TICKER if (extras.containsKey("ticker")) { notificationTicker = extras.getString("ticker"); } else { notificationTicker = notificationText; } // SOUND if (extras.containsKey("sound")) { if (extras.getString("sound").equalsIgnoreCase("default")) { notificationDefaults |= Notification.DEFAULT_SOUND; } else { File file = null; // getResourcesDirectory file = new File("app://" + extras.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getResourcesDirectory + sound folder file = new File("app://sound/" + extras.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getExternalStorageDirectory file = new File("appdata://" + extras.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getExternalStorageDirectory + sound folder file = new File("appdata://sound/" + extras.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // res folder int soundId = appContext.getResources().getIdentifier(extras.getString("sound"), "raw", appContext.getPackageName()); if (soundId != 0) { notificationSound = Uri.parse("android.resource://" + appContext.getPackageName() + "/raw/" + soundId); } else { // res folder without file extension String soundFile = extras.getString("sound").split("\\.")[0]; soundId = appContext.getResources().getIdentifier(soundFile, "raw", appContext.getPackageName()); if (soundId != 0) { notificationSound = Uri.parse("android.resource://" + appContext.getPackageName() + "/raw/" + soundId); } } } } } } } } else if (extrasRoot.containsKey("sound")) { if (extrasRoot.getString("sound").equalsIgnoreCase("default")) { notificationDefaults |= Notification.DEFAULT_SOUND; } else { File file = null; // getResourcesDirectory file = new File("app://" + extrasRoot.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getResourcesDirectory + sound folder file = new File("app://sound/" + extrasRoot.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getExternalStorageDirectory file = new File("appdata://" + extrasRoot.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // getExternalStorageDirectory + sound folder file = new File("appdata://sound/" + extrasRoot.getString("sound")); if (file != null && file.exists()) { notificationSound = Uri.fromFile(file); } else { // res folder int soundId = appContext.getResources().getIdentifier(extrasRoot.getString("sound"), "raw", appContext.getPackageName()); if (soundId != 0) { notificationSound = Uri.parse("android.resource://" + appContext.getPackageName() + "/raw/" + soundId); } else { // res folder without file extension String soundFile = extrasRoot.getString("sound").split("\\.")[0]; soundId = appContext.getResources().getIdentifier(soundFile, "raw", appContext.getPackageName()); if (soundId != 0) { notificationSound = Uri.parse("android.resource://" + appContext.getPackageName() + "/raw/" + soundId); } } } } } } } } // VIBRATE if (extras.containsKey("vibrate") && extras.getString("vibrate").equalsIgnoreCase("true")) { notificationDefaults |= Notification.DEFAULT_VIBRATE; } // LIGHTS if (extras.containsKey("lights") && extras.getString("lights").equalsIgnoreCase("true")) { notificationDefaults |= Notification.DEFAULT_LIGHTS; } // NOTIFICATION ID if (extras.containsKey("notificationId")) { try { notificationId = Integer.parseInt(extras.getString("notificationId")); } catch (NumberFormatException nfe) { } } if (notificationId == 0) { notificationId = appContext.getAppProperties().getInt(PROPERTY_NOTIFICATION_ID, 0); notificationId++; appContext.getAppProperties().setInt(PROPERTY_NOTIFICATION_ID, notificationId); } // BADGE if (extras.containsKey("badge")) { try { badgeCount = Integer.parseInt(extras.getString("badge")); } catch (NumberFormatException nfe) { } } // LARGE ICON Bitmap largeIcon = null; if (extras.containsKey("largeIcon")) { largeIcon = getBitmap(extras.getString("largeIcon")); } else if (extras.containsKey("licon")) { largeIcon = getBitmap(extras.getString("licon")); } else if (extrasRoot.containsKey("largeIcon")) { largeIcon = getBitmap(extrasRoot.getString("largeIcon")); } else if (extrasRoot.containsKey("licon")) { largeIcon = getBitmap(extrasRoot.getString("licon")); } // SMALL ICON if (extras.containsKey("smallIcon")) { appIconId = appContext.getResources().getIdentifier(extras.getString("smallIcon"), "drawable", appContext.getPackageName()); if (appIconId == 0) { Log.i(TAG, "Unable to find resource identifier to custom smallIcon : " + extras.getString("smallIcon")); } } else if (extras.containsKey("sicon")) { appIconId = appContext.getResources().getIdentifier(extras.getString("sicon"), "drawable", appContext.getPackageName()); if (appIconId == 0) { Log.i(TAG, "Unable to find resource identifier to custom sicon : " + extras.getString("sicon")); } } else if (extrasRoot.containsKey("smallIcon")) { appIconId = appContext.getResources().getIdentifier(extrasRoot.getString("smallIcon"), "drawable", appContext.getPackageName()); if (appIconId == 0) { Log.i(TAG, "Unable to find resource identifier to custom smallIcon : " + extrasRoot.getString("smallIcon")); } } else if (extrasRoot.containsKey("sicon")) { appIconId = appContext.getResources().getIdentifier(extrasRoot.getString("sicon"), "drawable", appContext.getPackageName()); if (appIconId == 0) { Log.i(TAG, "Unable to find resource identifier to custom smallIcon : " + extrasRoot.getString("sicon")); } } if (notificationText != null) { // Intent launch = getLauncherIntent(extras); Intent launch = new Intent(appContext, PendingNotificationActivity.class); launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); if (extrasRoot != null && !extrasRoot.isEmpty()) { launch.putExtra(PROPERTY_EXTRAS, extrasRoot); } launch.setAction("dummy_unique_action_identifyer:" + notificationId); PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, launch, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext); mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText)); mBuilder.setContentText(notificationText); if (notificationTitle != null) { mBuilder.setContentTitle(notificationTitle); } if (notificationTicker != null) { mBuilder.setTicker(notificationTicker); } if (notificationDefaults != 0) { mBuilder.setDefaults(notificationDefaults); } if (notificationSound != null) { mBuilder.setSound(notificationSound); } if (badgeCount >= 0) { mBuilder.setNumber(badgeCount); } if (largeIcon != null) { mBuilder.setLargeIcon(largeIcon); } if (appIconId == 0) { appIconId = appContext.getResources().getIdentifier("appicon", "drawable", appContext.getPackageName()); } mBuilder.setSmallIcon(appIconId); mBuilder.setContentIntent(contentIntent); mBuilder.setAutoCancel(true); mBuilder.setWhen(System.currentTimeMillis()); // ledARGB, ledOnMS, ledOffMS boolean customLight = false; int argb = 0xFFFFFFFF; int onMs = 1000; int offMs = 2000; if (extras.containsKey("ledARGB")) { try { argb = TiColorHelper.parseColor(extras.getString("ledARGB")); customLight = true; } catch (Exception ex) { try { argb = Integer.parseInt(extras.getString("ledARGB")); customLight = true; } catch (NumberFormatException nfe) { } } } else if (extras.containsKey("ledc")) { try { argb = TiColorHelper.parseColor(extras.getString("ledc")); customLight = true; } catch (Exception ex) { try { argb = Integer.parseInt(extras.getString("ledc")); customLight = true; } catch (NumberFormatException nfe) { } } } else if (extrasRoot.containsKey("ledARGB")) { try { argb = TiColorHelper.parseColor(extrasRoot.getString("ledARGB")); customLight = true; } catch (Exception ex) { try { argb = Integer.parseInt(extrasRoot.getString("ledARGB")); customLight = true; } catch (NumberFormatException nfe) { } } } else if (extrasRoot.containsKey("ledc")) { try { argb = TiColorHelper.parseColor(extrasRoot.getString("ledc")); customLight = true; } catch (Exception ex) { try { argb = Integer.parseInt(extrasRoot.getString("ledc")); customLight = true; } catch (NumberFormatException nfe) { } } } if (extras.containsKey("ledOnMS")) { try { onMs = Integer.parseInt(extras.getString("ledOnMS")); customLight = true; } catch (NumberFormatException nfe) { } } if (extras.containsKey("ledOffMS")) { try { offMs = Integer.parseInt(extras.getString("ledOffMS")); customLight = true; } catch (NumberFormatException nfe) { } } if (customLight) { mBuilder.setLights(argb, onMs, offMs); } //Visibility if (extras.containsKey("visibility")) { try { mBuilder.setVisibility(Integer.parseInt(extras.getString("visibility"))); } catch (NumberFormatException nfe) { } } else if (extras.containsKey("vis")) { try { mBuilder.setVisibility(Integer.parseInt(extras.getString("vis"))); } catch (NumberFormatException nfe) { } } else if (extrasRoot.containsKey("visibility")) { try { mBuilder.setVisibility(Integer.parseInt(extrasRoot.getString("visibility"))); } catch (NumberFormatException nfe) { } } else if (extrasRoot.containsKey("vis")) { try { mBuilder.setVisibility(Integer.parseInt(extrasRoot.getString("vis"))); } catch (NumberFormatException nfe) { } } //Icon background color int accent_argb = 0xFFFFFFFF; if (extras.containsKey("accentARGB")) { try { accent_argb = TiColorHelper.parseColor(extras.getString("accentARGB")); mBuilder.setColor(accent_argb); } catch (Exception ex) { try { accent_argb = Integer.parseInt(extras.getString("accentARGB")); mBuilder.setColor(accent_argb); } catch (NumberFormatException nfe) { } } } else if (extras.containsKey("bgac")) { try { accent_argb = TiColorHelper.parseColor(extras.getString("bgac")); mBuilder.setColor(accent_argb); } catch (Exception ex) { try { accent_argb = Integer.parseInt(extras.getString("bgac")); mBuilder.setColor(accent_argb); } catch (NumberFormatException nfe) { } } } else if (extrasRoot.containsKey("accentARGB")) { try { accent_argb = TiColorHelper.parseColor(extrasRoot.getString("accentARGB")); mBuilder.setColor(accent_argb); } catch (Exception ex) { try { accent_argb = Integer.parseInt(extrasRoot.getString("accentARGB")); mBuilder.setColor(accent_argb); } catch (NumberFormatException nfe) { } } } else if (extrasRoot.containsKey("bgac")) { try { accent_argb = TiColorHelper.parseColor(extrasRoot.getString("bgac")); mBuilder.setColor(accent_argb); } catch (Exception ex) { try { accent_argb = Integer.parseInt(extrasRoot.getString("bgac")); mBuilder.setColor(accent_argb); } catch (NumberFormatException nfe) { } } } NotificationManager nm = (NotificationManager) appContext .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(notificationId, mBuilder.build()); } }
From source file:com.hhunj.hhudata.ForegroundService.java
protected void showNotification(CharSequence from, CharSequence message) { // look up the notification manager service NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // The PendingIntent to launch our activity if the user selects this // notification //,/*from w w w . j av a 2 s. c om*/ PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, IncomingMessageView.class), 0); // The ticker text, this uses a formatted string so our message could be // localized String tickerText = getString(R.string.imcoming_message_ticker_text, message); // construct the Notification object. Notification notif = new Notification(R.drawable.stat_sample, tickerText, System.currentTimeMillis()); // Set the info for the views that show in the notification panel. notif.setLatestEventInfo(this, from, message, contentIntent); // // Date dt = new Date(); if (IsDuringWorkHours(dt)) { notif.defaults |= (Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); } else { notif.defaults |= (Notification.DEFAULT_VIBRATE); } // after a 100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms.// notif.vibrate = new long[] { 100, 250, 100, 500 }; nm.notify(R.string.imcoming_message_ticker_text, notif); }
From source file:com.bonsai.btcreceive.WalletService.java
private void showEventNotification(int noteId, int icon, String title, String msg) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(icon) .setContentTitle(title).setContentText(msg).setAutoCancel(true).setDefaults( Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); // Creates an explicit intent for an Activity in your app Intent intent = new Intent(this, 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 your // application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // 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(intent);/*w ww .j a v a 2s . c om*/ PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); mNM.notify(noteId, mBuilder.build()); }