Search in sources :

Example 86 with NotificationChannel

use of android.app.NotificationChannel in project robolectric by robolectric.

the class ShadowNotificationManagerTest method deleteNotificationChannelGroup.

@Test
@Config(minSdk = Build.VERSION_CODES.O)
public void deleteNotificationChannelGroup() {
    final String channelId = "channelId";
    final String channelGroupId = "channelGroupId";
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(channelGroupId, "groupName"));
    NotificationChannel channel = new NotificationChannel(channelId, "channelName", 1);
    channel.setGroup(channelGroupId);
    notificationManager.createNotificationChannel(channel);
    assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse();
    notificationManager.deleteNotificationChannelGroup(channelGroupId);
    assertThat(shadowOf(notificationManager).getNotificationChannelGroup(channelGroupId)).isNull();
    // Per documentation, deleting a channel group also deletes all associated channels.
    assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isTrue();
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 87 with NotificationChannel

use of android.app.NotificationChannel in project robolectric by robolectric.

the class ShadowNotificationManager method createNotificationChannel.

@Implementation(minSdk = Build.VERSION_CODES.O)
protected void createNotificationChannel(Object channel) {
    String id = ReflectionHelpers.callInstanceMethod(channel, "getId");
    // for more info.
    if (deletedNotificationChannels.containsKey(id)) {
        notificationChannels.put(id, deletedNotificationChannels.remove(id));
    }
    NotificationChannel existingChannel = (NotificationChannel) notificationChannels.get(id);
    // for more info.
    if (existingChannel != null) {
        NotificationChannel newChannel = (NotificationChannel) channel;
        existingChannel.setName(newChannel.getName());
        existingChannel.setDescription(newChannel.getDescription());
        if (newChannel.getImportance() < existingChannel.getImportance()) {
            existingChannel.setImportance(newChannel.getImportance());
        }
        if (Strings.isNullOrEmpty(existingChannel.getGroup())) {
            existingChannel.setGroup(newChannel.getGroup());
        }
        return;
    }
    notificationChannels.put(id, channel);
}
Also used : NotificationChannel(android.app.NotificationChannel) Implementation(org.robolectric.annotation.Implementation)

Example 88 with NotificationChannel

use of android.app.NotificationChannel in project ETSMobile-Android2 by ApplETS.

the class ETSFcmListenerService method sendNotification.

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param data FCM message received.
 */
private void sendNotification(Map<String, String> data) {
    SecurePreferences securePreferences = new SecurePreferences(this);
    Gson gson = new Gson();
    String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");
    ArrayList<MonETSNotification> receivedNotif = gson.fromJson(receivedNotifString, new TypeToken<ArrayList<MonETSNotification>>() {
    }.getType());
    if (receivedNotif == null) {
        receivedNotif = new ArrayList<>();
    }
    MonETSNotification nouvelleNotification = getMonETSNotificationFromMap(data);
    receivedNotif.add(nouvelleNotification);
    int numberOfNotifications = receivedNotif.size();
    Intent intent = new Intent(this, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = mNotificationManager.getNotificationChannel(Constants.DEFAULT_NOTIFICATION_CHANNEL_ID);
        if (channel == null) {
            // We could create multiple channels based on the notification but let's just create one for maintenance purposes.
            String channelName = getString(R.string.fcm_fallback_notification_channel_label);
            channel = new NotificationChannel(Constants.DEFAULT_NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
        }
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Constants.DEFAULT_NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
    NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
    String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
    inBoxStyle.setBigContentTitle(bigContentTitle);
    String username = ApplicationManager.userCredentials.getUsername();
    Spannable sb = new SpannableString(username);
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inBoxStyle.setSummaryText(sb);
    securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
    int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
    minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
    for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
        inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
    }
    if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
        int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
        String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
        Spannable others = new SpannableString(plusOthersString);
        others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inBoxStyle.addLine(others);
    }
    mBuilder.setStyle(inBoxStyle);
    mNotificationManager.notify(1, mBuilder.build());
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(android.app.NotificationManager) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) SpannableString(android.text.SpannableString) NotificationChannel(android.app.NotificationChannel) SpannableString(android.text.SpannableString) Bitmap(android.graphics.Bitmap) TypeToken(com.google.gson.reflect.TypeToken) StyleSpan(android.text.style.StyleSpan) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Spannable(android.text.Spannable)

Example 89 with NotificationChannel

use of android.app.NotificationChannel in project cordova-plugin-local-notifications by katzer.

the class Manager method createDefaultChannel.

/**
 * TODO: temporary
 */
@SuppressLint("WrongConstant")
private void createDefaultChannel() {
    NotificationManager mgr = getNotMgr();
    if (SDK_INT < O)
        return;
    NotificationChannel channel = mgr.getNotificationChannel(CHANNEL_ID);
    if (channel != null)
        return;
    channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT);
    mgr.createNotificationChannel(channel);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) SuppressLint(android.annotation.SuppressLint)

Example 90 with NotificationChannel

use of android.app.NotificationChannel in project Douya by DreaminginCodeZH.

the class SendBroadcastWriter method createNotificationChannel.

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return;
    }
    Context context = getContext();
    String channelName = context.getString(Notifications.Channels.SEND_BROADCAST.NAME_RES);
    @SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(Notifications.Channels.SEND_BROADCAST.ID, channelName, Notifications.Channels.SEND_BROADCAST.IMPORTANCE);
    String channelDescription = context.getString(Notifications.Channels.SEND_BROADCAST.DESCRIPTION_RES);
    channel.setDescription(channelDescription);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}
Also used : Context(android.content.Context) NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) SuppressLint(android.annotation.SuppressLint)

Aggregations

NotificationChannel (android.app.NotificationChannel)762 Test (org.junit.Test)430 NotificationBackend (com.android.settings.notification.NotificationBackend)215 NotificationManager (android.app.NotificationManager)210 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)114 Intent (android.content.Intent)87 Preference (androidx.preference.Preference)76 PendingIntent (android.app.PendingIntent)73 NotificationChannelGroup (android.app.NotificationChannelGroup)54 NotificationCompat (android.support.v4.app.NotificationCompat)45 Notification (android.app.Notification)43 ArrayList (java.util.ArrayList)34 TargetApi (android.annotation.TargetApi)32 AudioAttributes (android.media.AudioAttributes)31 RequiresApi (android.support.annotation.RequiresApi)28 RequiresApi (androidx.annotation.RequiresApi)25 Uri (android.net.Uri)21 RestrictedListPreference (com.android.settings.RestrictedListPreference)20 SuppressLint (android.annotation.SuppressLint)19 ShortcutInfo (android.content.pm.ShortcutInfo)19