use of androidx.core.app.NotificationManagerCompat in project Tusky by Vavassor.
the class NotificationHelper method make.
/**
* Takes a given Mastodon notification and either creates a new Android notification or updates
* the state of the existing notification to reflect the new interaction.
*
* @param context to access application preferences and services
* @param body a new Mastodon notification
* @param account the account for which the notification should be shown
*/
public static void make(final Context context, Notification body, AccountEntity account, boolean isFirstOfBatch) {
body = body.rewriteToStatusTypeIfNeeded(account.getAccountId());
if (!filterNotification(account, body, context)) {
return;
}
String rawCurrentNotifications = account.getActiveNotifications();
JSONArray currentNotifications;
try {
currentNotifications = new JSONArray(rawCurrentNotifications);
} catch (JSONException e) {
currentNotifications = new JSONArray();
}
for (int i = 0; i < currentNotifications.length(); i++) {
try {
if (currentNotifications.getString(i).equals(body.getAccount().getName())) {
currentNotifications.remove(i);
break;
}
} catch (JSONException e) {
Log.d(TAG, Log.getStackTraceString(e));
}
}
currentNotifications.put(body.getAccount().getName());
account.setActiveNotifications(currentNotifications.toString());
// Notification group member
// =========================
final NotificationCompat.Builder builder = newNotification(context, body, account, false);
notificationId++;
builder.setContentTitle(titleForType(context, body, account)).setContentText(bodyForType(body, context));
if (body.getType() == Notification.Type.MENTION || body.getType() == Notification.Type.POLL) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(bodyForType(body, context)));
}
// load the avatar synchronously
Bitmap accountAvatar;
try {
FutureTarget<Bitmap> target = Glide.with(context).asBitmap().load(body.getAccount().getAvatar()).transform(new RoundedCorners(20)).submit();
accountAvatar = target.get();
} catch (ExecutionException | InterruptedException e) {
Log.d(TAG, "error loading account avatar", e);
accountAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar_default);
}
builder.setLargeIcon(accountAvatar);
// Reply to mention action; RemoteInput is available from KitKat Watch, but buttons are available from Nougat
if (body.getType() == Notification.Type.MENTION && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
RemoteInput replyRemoteInput = new RemoteInput.Builder(KEY_REPLY).setLabel(context.getString(R.string.label_quick_reply)).build();
PendingIntent quickReplyPendingIntent = getStatusReplyIntent(REPLY_ACTION, context, body, account);
NotificationCompat.Action quickReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_24dp, context.getString(R.string.action_quick_reply), quickReplyPendingIntent).addRemoteInput(replyRemoteInput).build();
builder.addAction(quickReplyAction);
PendingIntent composePendingIntent = getStatusReplyIntent(COMPOSE_ACTION, context, body, account);
NotificationCompat.Action composeAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_24dp, context.getString(R.string.action_compose_shortcut), composePendingIntent).build();
builder.addAction(composeAction);
}
builder.setSubText(account.getFullName());
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
builder.setOnlyAlertOnce(true);
// only alert for the first notification of a batch to avoid multiple alerts at once
if (!isFirstOfBatch) {
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
}
// Summary
// =======
final NotificationCompat.Builder summaryBuilder = newNotification(context, body, account, true);
if (currentNotifications.length() != 1) {
try {
String title = context.getResources().getQuantityString(R.plurals.notification_title_summary, currentNotifications.length(), currentNotifications.length());
String text = joinNames(context, currentNotifications);
summaryBuilder.setContentTitle(title).setContentText(text);
} catch (JSONException e) {
Log.d(TAG, Log.getStackTraceString(e));
}
}
summaryBuilder.setSubText(account.getFullName());
summaryBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
summaryBuilder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
summaryBuilder.setOnlyAlertOnce(true);
summaryBuilder.setGroupSummary(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, builder.build());
if (currentNotifications.length() == 1) {
notificationManager.notify((int) account.getId(), builder.setGroupSummary(true).build());
} else {
notificationManager.notify((int) account.getId(), summaryBuilder.build());
}
}
use of androidx.core.app.NotificationManagerCompat in project AntennaPod by AntennaPod.
the class NotificationUtils method createChannels.
public static void createChannels(final Context context) {
final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context);
final List<NotificationChannelGroupCompat> channelGroups = Arrays.asList(createGroupErrors(context), createGroupNews(context));
mNotificationManager.createNotificationChannelGroupsCompat(channelGroups);
final List<NotificationChannelCompat> channels = Arrays.asList(createChannelUserAction(context), createChannelDownloading(context), createChannelPlaying(context), createChannelError(context), createChannelSyncError(context), createChannelAutoDownload(context), createChannelEpisodeNotification(context));
mNotificationManager.createNotificationChannelsCompat(channels);
}
use of androidx.core.app.NotificationManagerCompat in project AntennaPod by AntennaPod.
the class PlaybackService method onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "OnStartCommand called");
stateManager.startForeground(R.id.notification_playing, notificationBuilder.build());
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(R.id.notification_streaming_confirmation);
final int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
final boolean hardwareButton = intent.getBooleanExtra(MediaButtonReceiver.EXTRA_HARDWAREBUTTON, false);
Playable playable = intent.getParcelableExtra(EXTRA_PLAYABLE);
if (keycode == -1 && playable == null) {
Log.e(TAG, "PlaybackService was started with no arguments");
stateManager.stopService();
return Service.START_NOT_STICKY;
}
if ((flags & Service.START_FLAG_REDELIVERY) != 0) {
Log.d(TAG, "onStartCommand is a redelivered intent, calling stopForeground now.");
stateManager.stopForeground(true);
} else {
if (keycode != -1) {
boolean notificationButton;
if (hardwareButton) {
Log.d(TAG, "Received hardware button event");
notificationButton = false;
} else {
Log.d(TAG, "Received media button event");
notificationButton = true;
}
boolean handled = handleKeycode(keycode, notificationButton);
if (!handled && !stateManager.hasReceivedValidStartCommand()) {
stateManager.stopService();
return Service.START_NOT_STICKY;
}
} else {
stateManager.validStartCommandWasReceived();
boolean stream = intent.getBooleanExtra(EXTRA_SHOULD_STREAM, true);
boolean allowStreamThisTime = intent.getBooleanExtra(EXTRA_ALLOW_STREAM_THIS_TIME, false);
boolean allowStreamAlways = intent.getBooleanExtra(EXTRA_ALLOW_STREAM_ALWAYS, false);
boolean startWhenPrepared = intent.getBooleanExtra(EXTRA_START_WHEN_PREPARED, false);
boolean prepareImmediately = intent.getBooleanExtra(EXTRA_PREPARE_IMMEDIATELY, false);
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
if (allowStreamAlways) {
UserPreferences.setAllowMobileStreaming(true);
}
boolean localFeed = URLUtil.isContentUrl(playable.getStreamUrl());
if (stream && !NetworkUtils.isStreamingAllowed() && !allowStreamThisTime && !localFeed) {
displayStreamingNotAllowedNotification(intent);
PlaybackPreferences.writeNoMediaPlaying();
stateManager.stopService();
return Service.START_NOT_STICKY;
}
Observable.fromCallable(() -> {
if (playable instanceof FeedMedia) {
return DBReader.getFeedMedia(((FeedMedia) playable).getId());
} else {
return playable;
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(playableLoaded -> {
if (!playable.getIdentifier().equals(PlaybackPreferences.getCurrentlyPlayingFeedMediaId())) {
PlaybackPreferences.clearCurrentlyPlayingTemporaryPlaybackSpeed();
}
mediaPlayer.playMediaObject(playableLoaded, stream, startWhenPrepared, prepareImmediately);
addPlayableToQueue(playableLoaded);
}, error -> {
Log.d(TAG, "Playable was not found. Stopping service.");
error.printStackTrace();
stateManager.stopService();
});
return Service.START_NOT_STICKY;
}
}
return Service.START_NOT_STICKY;
}
use of androidx.core.app.NotificationManagerCompat in project AntennaPod by AntennaPod.
the class NewEpisodesNotification method showIfNeeded.
public void showIfNeeded(Context context, Feed feed) {
FeedPreferences prefs = feed.getPreferences();
if (!prefs.getKeepUpdated() || !prefs.getShowEpisodeNotification()) {
return;
}
int newEpisodesBefore = countersBefore.get(feed.getId());
int newEpisodesAfter = getNewEpisodeCount(feed.getId());
Log.d(TAG, "New episodes before: " + newEpisodesBefore + ", after: " + newEpisodesAfter);
if (newEpisodesAfter > newEpisodesBefore) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
showNotification(newEpisodesAfter, feed, context, notificationManager);
}
}
use of androidx.core.app.NotificationManagerCompat in project RSAndroidApp by RailwayStations.
the class NearbyNotificationService method onStartCommand.
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
cancelNotification();
Log.i(TAG, "Received start command");
final Intent resultIntent = new Intent(this, MainActivity.class);
final PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
NearbyBahnhofNotificationManager.createChannel(this);
// show a permanent notification to indicate that position detection is running
final Notification ongoingNotification = new NotificationCompat.Builder(this, NearbyBahnhofNotificationManager.CHANNEL_ID).setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.nearby_notification_active)).setOngoing(true).setLocalOnly(true).setContentIntent(resultPendingIntent).build();
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification);
registerLocationManager();
return START_STICKY;
}
Aggregations