use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.
the class NotificationUtils method notifySecondMentions.
public static void notifySecondMentions(Context context, int secondAccount) {
MentionsDataSource data = MentionsDataSource.getInstance(context);
int numberNew = data.getUnreadCount(secondAccount);
int smallIcon = R.drawable.ic_stat_icon;
Bitmap largeIcon;
Intent resultIntent = new Intent(context, SwitchAccountsRedirect.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
NotificationCompat.Builder mBuilder;
String title = context.getResources().getString(R.string.app_name) + " - " + context.getResources().getString(R.string.sec_acc);
;
String name = null;
String message;
String messageLong;
String tweetText = null;
NotificationCompat.Action replyAction = null;
if (numberNew == 1) {
name = data.getNewestName(secondAccount);
SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
// then just quit
if (sharedPrefs.getString("muted_users", "").contains(name) && !sharedPrefs.getBoolean("show_muted_mentions", false)) {
return;
}
message = context.getResources().getString(R.string.mentioned_by) + " @" + name;
tweetText = data.getNewestMessage(secondAccount);
messageLong = "<b>@" + name + "</b>: " + tweetText;
largeIcon = getImage(context, name);
Intent reply = new Intent(context, NotificationComposeSecondAcc.class);
sharedPrefs.edit().putString("from_notification_second", "@" + name).commit();
long id = data.getLastIds(secondAccount)[0];
PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0);
sharedPrefs.edit().putLong("from_notification_long_second", id).commit();
sharedPrefs.edit().putString("from_notification_text_second", "@" + name + ": " + TweetLinkUtils.removeColorHtml(tweetText, AppSettings.getInstance(context))).commit();
// Create the remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("@" + name + " ").build();
// Create the notification action
replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply), replyPending).addRemoteInput(remoteInput).build();
} else {
// more than one mention
message = numberNew + " " + context.getResources().getString(R.string.new_mentions);
messageLong = "<b>" + context.getResources().getString(R.string.mentions) + "</b>: " + numberNew + " " + context.getResources().getString(R.string.new_mentions);
largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
}
Intent markRead = new Intent(context, MarkReadSecondAccService.class);
PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);
AppSettings settings = AppSettings.getInstance(context);
Intent deleteIntent = new Intent(context, NotificationDeleteReceiverTwo.class);
mBuilder = new NotificationCompat.Builder(context).setContentTitle(title).setContentText(TweetLinkUtils.removeColorHtml(message, settings)).setSmallIcon(smallIcon).setLargeIcon(largeIcon).setContentIntent(resultPendingIntent).setAutoCancel(true).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
if (numberNew == 1) {
mBuilder.addAction(replyAction);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? messageLong.replaceAll("FF8800", settings.accentColor) : messageLong)));
} else {
NotificationCompat.InboxStyle inbox = getMentionsInboxStyle(numberNew, secondAccount, context, TweetLinkUtils.removeColorHtml(message, settings));
mBuilder.setStyle(inbox);
}
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.notifications) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(9, mBuilder.build());
// if we want to wake the screen on a new message
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 (context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0).getBoolean("pebble_notification", false)) {
sendAlertToPebble(context, title, messageLong);
}
// Light Flow notification
sendToLightFlow(context, title, messageLong);
}
}
use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.
the class NotificationUtils method makeFavsNotification.
public static void makeFavsNotification(ArrayList<String[]> tweets, Context context, boolean toDrawer) {
String shortText;
String longText;
String title;
int smallIcon = R.drawable.ic_stat_icon;
Bitmap largeIcon;
Intent resultIntent;
if (toDrawer) {
resultIntent = new Intent(context, RedirectToDrawer.class);
} else {
resultIntent = new Intent(context, NotiTweetPager.class);
}
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
NotificationCompat.InboxStyle inbox = null;
if (tweets.size() == 1) {
title = tweets.get(0)[0];
shortText = tweets.get(0)[1];
longText = shortText;
largeIcon = getImage(context, tweets.get(0)[2]);
} else {
inbox = new NotificationCompat.InboxStyle();
title = context.getResources().getString(R.string.favorite_users);
shortText = tweets.size() + " " + context.getResources().getString(R.string.fav_user_tweets);
longText = "";
try {
inbox.setBigContentTitle(shortText);
} catch (Exception e) {
}
if (tweets.size() <= 5) {
for (String[] s : tweets) {
inbox.addLine(Html.fromHtml("<b>" + s[0] + ":</b> " + s[1]));
}
} else {
for (int i = 0; i < 5; i++) {
inbox.addLine(Html.fromHtml("<b>" + tweets.get(i)[0] + ":</b> " + tweets.get(i)[1]));
}
inbox.setSummaryText("+" + (tweets.size() - 5) + " " + context.getString(R.string.tweets));
}
largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
}
NotificationCompat.Builder mBuilder;
AppSettings settings = AppSettings.getInstance(context);
if (shortText.contains("@" + settings.myScreenName)) {
// return because there is a mention notification for this already
return;
}
Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);
mBuilder = new NotificationCompat.Builder(context).setContentTitle(title).setContentText(TweetLinkUtils.removeColorHtml(shortText, settings)).setSmallIcon(smallIcon).setLargeIcon(largeIcon).setContentIntent(resultPendingIntent).setAutoCancel(true).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
if (inbox == null) {
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? longText.replaceAll("FF8800", settings.accentColor) : longText)));
} else {
mBuilder.setStyle(inbox);
}
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.notifications) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(2, mBuilder.build());
// if we want to wake the screen on a new message
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 (context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0).getBoolean("pebble_notification", false)) {
sendAlertToPebble(context, title, shortText);
}
// Light Flow notification
sendToLightFlow(context, title, shortText);
}
}
use of android.support.v4.app.NotificationManagerCompat in project Reader by TheKeeperOfPie.
the class Receiver method checkInbox.
public void checkInbox(final Context context, @Nullable final ArrayList<String> names) {
final ArrayList<String> readNames;
if (names == null) {
readNames = new ArrayList<>();
} else {
readNames = names;
}
new Thread(new Runnable() {
@Override
public void run() {
final Listing messages = new Listing();
Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
for (Account account : accounts) {
final AccountManagerFuture<Bundle> futureAuth = accountManager.getAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, null, true, null, null);
try {
Bundle bundle = futureAuth.getResult();
final String tokenAuth = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/message/unread").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
String response = okHttpClient.newCall(request).execute().body().string();
Listing listing = Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class));
messages.addChildren(listing.getChildren());
Log.d(TAG, account.name + " checkInbox response: " + response);
} catch (OperationCanceledException | AuthenticatorException | IOException e) {
e.printStackTrace();
}
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Thing thing = null;
for (int index = 0; index < messages.getChildren().size(); index++) {
thing = messages.getChildren().get(index);
if (readNames.contains(thing.getName())) {
reddit.markRead(thing.getName()).subscribe(new ObserverEmpty<>());
thing = null;
} else {
readNames.add(thing.getName());
break;
}
}
if (thing == null) {
notificationManager.cancel(NOTIFICATION_INBOX);
return;
}
int titleSuffixResource = messages.getChildren().size() == 1 ? R.string.new_message : R.string.new_messages;
CharSequence content = "";
CharSequence author = "";
CharSequence dest = "";
if (thing instanceof Message) {
content = ((Message) thing).getBodyHtml();
author = ((Message) thing).getAuthor();
dest = ((Message) thing).getDest();
} else if (thing instanceof Comment) {
content = ((Comment) thing).getBodyHtml();
author = ((Comment) thing).getAuthor();
dest = ((Comment) thing).getDest();
}
Intent intentActivity = new Intent(context, ActivityMain.class);
intentActivity.putExtra(ActivityMain.ACCOUNT, dest);
intentActivity.putExtra(ActivityMain.NAV_ID, R.id.item_inbox);
intentActivity.putExtra(ActivityMain.NAV_PAGE, ControllerInbox.UNREAD);
PendingIntent pendingIntentActivity = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_CANCEL_CURRENT);
Intent intentRecheckInbox = new Intent(INTENT_INBOX);
intentRecheckInbox.putExtra(READ_NAMES, readNames);
PendingIntent pendingIntentRecheckInbox = PendingIntent.getBroadcast(context, 0, intentRecheckInbox, PendingIntent.FLAG_CANCEL_CURRENT);
Themer themer = new Themer(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.app_icon_white_outline).setContentTitle(messages.getChildren().size() + " " + context.getResources().getString(titleSuffixResource)).setContentText(context.getString(R.string.expand_to_read_first_message)).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(context.getString(R.string.from) + " /u/" + author).bigText(content)).setContentIntent(pendingIntentActivity).addAction(new NotificationCompat.Action(R.drawable.ic_check_white_24dp, context.getString(R.string.mark_read), pendingIntentRecheckInbox)).setDeleteIntent(pendingIntentRecheckInbox).setAutoCancel(true).setCategory(NotificationCompat.CATEGORY_EMAIL).setColor(themer.getColorPrimary()).setLights(themer.getColorPrimary(), LED_MS_ON, LED_MS_OFF);
notificationManager.notify(NOTIFICATION_INBOX, builder.build());
}
}).start();
}
use of android.support.v4.app.NotificationManagerCompat in project SeriesGuide by UweTrottmann.
the class NotificationActionReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.hasExtra(EXTRA_EPISODE_TVDBID)) {
return;
}
int episodeTvdbvId = intent.getIntExtra(EXTRA_EPISODE_TVDBID, -1);
if (episodeTvdbvId <= 0) {
return;
}
Context appContext = context.getApplicationContext();
if (!(appContext instanceof SgApp)) {
return;
}
SgApp app = (SgApp) appContext;
// query for episode details
Cursor query = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodeWithShowUri(episodeTvdbvId), new String[] { SeriesGuideContract.Shows.REF_SHOW_ID, SeriesGuideContract.Episodes.SEASON, SeriesGuideContract.Episodes.NUMBER }, null, null, null);
if (query == null) {
return;
}
if (query.moveToFirst()) {
int showTvdbId = query.getInt(0);
int season = query.getInt(1);
int episode = query.getInt(2);
// mark episode watched
EpisodeTools.episodeWatched(app, showTvdbId, episodeTvdbvId, season, episode, EpisodeFlags.WATCHED);
}
query.close();
// dismiss the notification
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.cancel(SgApp.NOTIFICATION_EPISODE_ID);
// replicate delete intent
NotificationService.handleDeleteIntent(context, intent);
}
use of android.support.v4.app.NotificationManagerCompat in project SeriesGuide by UweTrottmann.
the class NotificationService method onNotify.
private void onNotify(final Cursor upcomingEpisodes, List<Integer> notifyPositions, long latestAirtime) {
final Context context = getApplicationContext();
CharSequence tickerText;
CharSequence contentTitle;
CharSequence contentText;
PendingIntent contentIntent;
// base intent for task stack
final Intent showsIntent = new Intent(context, ShowsActivity.class);
showsIntent.putExtra(ShowsActivity.InitBundle.SELECTED_TAB, ShowsActivity.InitBundle.INDEX_TAB_UPCOMING);
final int count = notifyPositions.size();
if (count == 1) {
// notify in detail about one episode
upcomingEpisodes.moveToPosition(notifyPositions.get(0));
final String showTitle = upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE);
// show title and number, like 'Show 1x01'
contentTitle = TextTools.getShowWithEpisodeNumber(this, showTitle, upcomingEpisodes.getInt(NotificationQuery.SEASON), upcomingEpisodes.getInt(NotificationQuery.NUMBER));
tickerText = getString(R.string.upcoming_show, contentTitle);
// "8:00 PM on Network"
final String releaseTime = TimeTools.formatToLocalTime(this, TimeTools.applyUserOffset(this, upcomingEpisodes.getLong(NotificationQuery.EPISODE_FIRST_RELEASE_MS)));
final String network = upcomingEpisodes.getString(NotificationQuery.NETWORK);
contentText = getString(R.string.upcoming_show_detailed, releaseTime, network);
Intent episodeDetailsIntent = new Intent(context, EpisodesActivity.class);
episodeDetailsIntent.putExtra(EpisodesActivity.InitBundle.EPISODE_TVDBID, upcomingEpisodes.getInt(NotificationQuery._ID));
episodeDetailsIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);
contentIntent = TaskStackBuilder.create(context).addNextIntent(showsIntent).addNextIntent(episodeDetailsIntent).getPendingIntent(REQUEST_CODE_SINGLE_EPISODE, PendingIntent.FLAG_CANCEL_CURRENT);
} else {
// notify about multiple episodes
tickerText = getString(R.string.upcoming_episodes);
contentTitle = getString(R.string.upcoming_episodes_number, count);
contentText = getString(R.string.upcoming_display);
contentIntent = TaskStackBuilder.create(context).addNextIntent(showsIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime)).getPendingIntent(REQUEST_CODE_MULTIPLE_EPISODES, PendingIntent.FLAG_CANCEL_CURRENT);
}
final NotificationCompat.Builder nb = new NotificationCompat.Builder(context);
boolean richNotification = AndroidUtils.isJellyBeanOrHigher();
if (richNotification) {
// JELLY BEAN and above
if (count == 1) {
// single episode
upcomingEpisodes.moveToPosition(notifyPositions.get(0));
maybeSetPoster(nb, upcomingEpisodes.getString(NotificationQuery.POSTER));
if (!DisplaySettings.preventSpoilers(context)) {
final String episodeTitle = upcomingEpisodes.getString(NotificationQuery.TITLE);
final String episodeSummary = upcomingEpisodes.getString(NotificationQuery.OVERVIEW);
final SpannableStringBuilder bigText = new SpannableStringBuilder();
bigText.append(TextUtils.isEmpty(episodeTitle) ? "" : episodeTitle);
bigText.setSpan(new StyleSpan(Typeface.BOLD), 0, bigText.length(), 0);
bigText.append("\n");
bigText.append(TextUtils.isEmpty(episodeSummary) ? "" : episodeSummary);
nb.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText).setSummaryText(contentText));
}
// Action button to check in
Intent checkInActionIntent = new Intent(context, QuickCheckInActivity.class);
checkInActionIntent.putExtra(QuickCheckInActivity.InitBundle.EPISODE_TVDBID, upcomingEpisodes.getInt(NotificationQuery._ID));
checkInActionIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);
checkInActionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
PendingIntent checkInIntent = PendingIntent.getActivity(context, REQUEST_CODE_ACTION_CHECKIN, checkInActionIntent, PendingIntent.FLAG_CANCEL_CURRENT);
nb.addAction(R.drawable.ic_action_checkin, getString(R.string.checkin), checkInIntent);
// Action button to set watched
Intent setWatchedIntent = new Intent(context, NotificationActionReceiver.class);
setWatchedIntent.putExtra(NotificationActionReceiver.EXTRA_EPISODE_TVDBID, upcomingEpisodes.getInt(NotificationQuery._ID));
// data to handle delete
checkInActionIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);
PendingIntent setWatchedPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_ACTION_SET_WATCHED, setWatchedIntent, PendingIntent.FLAG_CANCEL_CURRENT);
nb.addAction(R.drawable.ic_action_tick, getString(R.string.action_watched), setWatchedPendingIntent);
nb.setNumber(1);
} else {
// multiple episodes
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// display at most the first five
for (int displayIndex = 0; displayIndex < Math.min(count, 5); displayIndex++) {
if (!upcomingEpisodes.moveToPosition(notifyPositions.get(displayIndex))) {
// could not go to the desired position (testing just in case)
break;
}
final SpannableStringBuilder lineText = new SpannableStringBuilder();
// show title and number, like 'Show 1x01'
String title = TextTools.getShowWithEpisodeNumber(this, upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE), upcomingEpisodes.getInt(NotificationQuery.SEASON), upcomingEpisodes.getInt(NotificationQuery.NUMBER));
lineText.append(title);
lineText.setSpan(new StyleSpan(Typeface.BOLD), 0, lineText.length(), 0);
lineText.append(" ");
// "8:00 PM on Network"
String releaseTime = TimeTools.formatToLocalTime(this, TimeTools.applyUserOffset(this, upcomingEpisodes.getLong(NotificationQuery.EPISODE_FIRST_RELEASE_MS)));
String network = upcomingEpisodes.getString(NotificationQuery.NETWORK);
lineText.append(getString(R.string.upcoming_show_detailed, releaseTime, network));
inboxStyle.addLine(lineText);
}
// tell if we could not display all episodes
if (count > 5) {
inboxStyle.setSummaryText(getString(R.string.more, count - 5));
}
nb.setStyle(inboxStyle);
nb.setNumber(count);
}
} else {
// ICS and below
if (count == 1) {
// single episode
upcomingEpisodes.moveToPosition(notifyPositions.get(0));
maybeSetPoster(nb, upcomingEpisodes.getString(NotificationQuery.POSTER));
}
}
// notification sound
final String ringtoneUri = NotificationSettings.getNotificationsRingtone(context);
// If the string is empty, the user chose silent...
boolean hasSound = ringtoneUri.length() != 0;
if (hasSound) {
// ...otherwise set the specified ringtone
nb.setSound(Uri.parse(ringtoneUri));
}
// vibration
boolean vibrates = NotificationSettings.isNotificationVibrating(context);
if (vibrates) {
nb.setVibrate(VIBRATION_PATTERN);
}
nb.setDefaults(Notification.DEFAULT_LIGHTS);
nb.setWhen(System.currentTimeMillis());
nb.setAutoCancel(true);
nb.setTicker(tickerText);
nb.setContentTitle(contentTitle);
nb.setContentText(contentText);
nb.setContentIntent(contentIntent);
nb.setSmallIcon(R.drawable.ic_notification);
nb.setColor(ContextCompat.getColor(this, R.color.accent_primary));
nb.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent i = new Intent(this, NotificationService.class);
i.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);
PendingIntent deleteIntent = PendingIntent.getService(this, REQUEST_CODE_DELETE_INTENT, i, PendingIntent.FLAG_CANCEL_CURRENT);
nb.setDeleteIntent(deleteIntent);
// build the notification
Notification notification = nb.build();
// use a unique id within the app
NotificationManagerCompat nm = NotificationManagerCompat.from(getApplicationContext());
nm.notify(SgApp.NOTIFICATION_EPISODE_ID, notification);
Timber.d("Notification: count=%d, rich(JB+)=%s, sound=%s, vibrate=%s, delete=%d", count, richNotification ? "YES" : "NO", hasSound ? "YES" : "NO", vibrates ? "YES" : "NO", latestAirtime);
}
Aggregations