Search in sources :

Example 56 with NotificationChannel

use of android.app.NotificationChannel in project ring-client-android by savoirfairelinux.

the class NotificationServiceImpl method registerNotificationChannels.

@RequiresApi(api = Build.VERSION_CODES.O)
private void registerNotificationChannels() {
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null)
        return;
    // Call channel
    NotificationChannel callChannel = new NotificationChannel(NOTIF_CHANNEL_CALL, mContext.getString(R.string.notif_channel_calls), NotificationManager.IMPORTANCE_HIGH);
    callChannel.enableVibration(true);
    callChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    notificationManager.createNotificationChannel(callChannel);
    // Text messages channel
    AudioAttributes soundAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build();
    NotificationChannel messageChannel = new NotificationChannel(NOTIF_CHANNEL_MESSAGE, mContext.getString(R.string.notif_channel_messages), NotificationManager.IMPORTANCE_HIGH);
    messageChannel.enableVibration(true);
    messageChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    messageChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), soundAttributes);
    notificationManager.createNotificationChannel(messageChannel);
    // Contact requests
    NotificationChannel requestsChannel = new NotificationChannel(NOTIF_CHANNEL_REQUEST, mContext.getString(R.string.notif_channel_requests), NotificationManager.IMPORTANCE_DEFAULT);
    requestsChannel.enableVibration(true);
    requestsChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    requestsChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), soundAttributes);
    notificationManager.createNotificationChannel(requestsChannel);
    // File transfer requests
    NotificationChannel fileTransferChannel = new NotificationChannel(NOTIF_CHANNEL_FILE_TRANSFER, mContext.getString(R.string.notif_channel_file_transfer), NotificationManager.IMPORTANCE_DEFAULT);
    fileTransferChannel.enableVibration(true);
    fileTransferChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    fileTransferChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), soundAttributes);
    notificationManager.createNotificationChannel(fileTransferChannel);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) AudioAttributes(android.media.AudioAttributes) RequiresApi(android.support.annotation.RequiresApi)

Example 57 with NotificationChannel

use of android.app.NotificationChannel in project AppCenter-SDK-Android by Microsoft.

the class PushNotifier method handleNotification.

/**
 * Builds a push notification using the given context and intent.
 *
 * @param context    The current context.
 * @param pushIntent The intent that is associated with the push.
 */
static void handleNotification(Context context, Intent pushIntent) throws RuntimeException {
    context = context.getApplicationContext();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    /* Generate notification identifier using the hash of the Google message id. */
    String messageId = PushIntentUtils.getGoogleMessageId(pushIntent);
    if (messageId == null) {
        AppCenterLog.error(LOG_TAG, "Push notification did not" + "contain Google message ID; aborting notification processing.");
        return;
    }
    int notificationId = messageId.hashCode();
    /* Click action. */
    PackageManager packageManager = context.getPackageManager();
    Intent actionIntent = packageManager.getLaunchIntentForPackage(context.getPackageName());
    if (actionIntent != null) {
        actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Map<String, String> customData = PushIntentUtils.getCustomData(pushIntent);
        for (String key : customData.keySet()) {
            actionIntent.putExtra(key, customData.get(key));
        }
        /* Set the message ID in the intent. */
        PushIntentUtils.setGoogleMessageId(messageId, actionIntent);
    } else {
        /* If no launcher, just create a placeholder action as the field is mandatory. */
        actionIntent = new Intent();
    }
    /* Get text. Use app name for title if title is missing. */
    String notificationTitle = PushIntentUtils.getTitle(pushIntent);
    if (notificationTitle == null || notificationTitle.isEmpty()) {
        notificationTitle = AppNameHelper.getAppName(context);
    }
    String notificationMessage = PushIntentUtils.getMessage(pushIntent);
    /* Start building notification. */
    Notification.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
        /* Get channel. */
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        /* Create or update channel. */
        // noinspection ConstantConditions
        notificationManager.createNotificationChannel(channel);
        /* And associate to notification. */
        builder = new Notification.Builder(context, channel.getId());
    } else {
        builder = getOldNotificationBuilder(context);
    }
    /* Set color. */
    setColor(pushIntent, builder);
    /* Set icon. */
    setIcon(context, pushIntent, builder);
    /* Set sound. */
    setSound(context, pushIntent, builder);
    /* Texts */
    builder.setContentTitle(notificationTitle).setContentText(notificationMessage).setWhen(System.currentTimeMillis());
    /* Click action. Reuse notification id for simplicity. */
    PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId, actionIntent, 0);
    builder.setContentIntent(contentIntent);
    /* Build method depends on versions. */
    Notification notification;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification = builder.build();
    } else {
        notification = getOldNotification(builder);
    }
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    // noinspection ConstantConditions
    notificationManager.notify(notificationId, notification);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Example 58 with NotificationChannel

use of android.app.NotificationChannel in project teamward-client by Neamar.

the class NotificationService method displayNotification.

/**
 * Create and show a simple notification containing the received GCM message.
 */
private void displayNotification(Account account, long gameId, int mapId) {
    Intent intent = new Intent(this, GameActivity.class);
    intent.putExtra("account", account);
    intent.putExtra("source", "notification");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, /* Request code */
    intent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID).setSmallIcon(R.drawable.ic_launcher_transparent_white).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_transparent)).setContentTitle(String.format(getString(R.string.welcome_to), getString(GameActivity.getMapName(mapId)))).setContentText(String.format(getString(R.string.player_is_in_game), account.summonerName)).setAutoCancel(true).setContentIntent(pendingIntent);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("notifications_new_game_vibrate", true)) {
        notificationBuilder.setVibrate(new long[] { 1000, 1000 });
    }
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
        Uri notificationUri = Uri.parse(prefs.getString("notifications_new_game_ringtone", Settings.System.DEFAULT_NOTIFICATION_URI.toString()));
        notificationBuilder.setSound(notificationUri);
    }
    NotificationManager notificationManager = getNotificationManager();
    boolean unableToDisplay = false;
    if (prefs.getBoolean("notifications_new_game", true)) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, getString(R.string.channel_in_game_notifications), NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }
            notificationManager.notify(Long.toString(gameId).hashCode(), notificationBuilder.build());
        } catch (RuntimeException e) {
            // Most likely, the ringtone doesn't exist anymore.
            // Used to happen only in Android 6.0, so the notification part is skipped on API M.
            // I'm keeping the try/catch just in case ;)
            unableToDisplay = true;
        }
        Tracker.trackNotificationDisplayed(this, account, mapId, getString(GameActivity.getMapName(mapId)), gameId, unableToDisplay);
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Uri(android.net.Uri)

Example 59 with NotificationChannel

use of android.app.NotificationChannel in project krypton-android by kryptco.

the class Notifications method setupNotificationChannels.

public static void setupNotificationChannels(Context context) {
    final String ACTION_REQUIRED_SILENT_CHANNEL_ID = ACTION_REQUIRED_CHANNEL_ID + "silent";
    final String APPROVED_SILENT_CHANNEL_ID = APPROVED_CHANNEL_ID + "silent";
    if (Build.VERSION.SDK_INT >= 26) {
        Uri defaultNotificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Delete old notification channels
        notificationManager.deleteNotificationChannel("action-required");
        notificationManager.deleteNotificationChannel("approved");
        {
            NotificationChannel actionRequiredChannel = new NotificationChannel(ACTION_REQUIRED_CHANNEL_ID, "Action Required", NotificationManager.IMPORTANCE_HIGH);
            actionRequiredChannel.setDescription("Requests that require your explicit approval.");
            actionRequiredChannel.setSound(defaultNotificationSound, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST).build());
            actionRequiredChannel.setVibrationPattern(new long[] { 0, 100, 100, 100 });
            notificationManager.createNotificationChannel(actionRequiredChannel);
        }
        {
            NotificationChannel actionRequiredSilentChannel = new NotificationChannel(ACTION_REQUIRED_SILENT_CHANNEL_ID, "Action Required Silent", NotificationManager.IMPORTANCE_HIGH);
            actionRequiredSilentChannel.setDescription("Requests that require your explicit approval.");
            actionRequiredSilentChannel.setSound(null, null);
            actionRequiredSilentChannel.enableVibration(false);
            notificationManager.createNotificationChannel(actionRequiredSilentChannel);
        }
        {
            NotificationChannel approvedChannel = new NotificationChannel(APPROVED_CHANNEL_ID, "Approved", NotificationManager.IMPORTANCE_HIGH);
            approvedChannel.setDescription("Automatically approved requests.");
            approvedChannel.setSound(defaultNotificationSound, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST).build());
            approvedChannel.setVibrationPattern(new long[] { 0, 100 });
            notificationManager.createNotificationChannel(approvedChannel);
        }
        {
            NotificationChannel approvedSilentChannel = new NotificationChannel(APPROVED_SILENT_CHANNEL_ID, "Approved Silent", NotificationManager.IMPORTANCE_HIGH);
            approvedSilentChannel.setDescription("Automatically approved requests.");
            approvedSilentChannel.setSound(null, null);
            approvedSilentChannel.enableVibration(false);
            notificationManager.createNotificationChannel(approvedSilentChannel);
        }
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) TaskStackBuilder(android.support.v4.app.TaskStackBuilder) Uri(android.net.Uri)

Example 60 with NotificationChannel

use of android.app.NotificationChannel in project LibreraReader by foobnix.

the class TTSNotification method initChannels.

@TargetApi(26)
public static void initChannels(Context context) {
    TTSNotification.context = context;
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel(DEFAULT, AppsConfig.TXT_APP_NAME, NotificationManager.IMPORTANCE_DEFAULT);
    // channel.setDescription("Channel description");
    notificationManager.createNotificationChannel(channel);
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) TargetApi(android.annotation.TargetApi)

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