Java Code Examples for android.app.NotificationManager#getNotificationChannel()

The following examples show how to use android.app.NotificationManager#getNotificationChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotificationUtils.java    From your-local-weather with GNU General Public License v3.0 7 votes vote down vote up
public static void checkAndCreateNotificationChannel(Context context) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel notificationChannel = notificationManager.getNotificationChannel("yourLocalWeather");
    boolean createNotification = notificationChannel == null;
    if (!createNotification &&
            ((notificationChannel.getImportance() == NotificationManager.IMPORTANCE_LOW) ||
                    (AppPreference.isVibrateEnabled(context) && (notificationChannel.getVibrationPattern() == null)))) {
        notificationManager.deleteNotificationChannel("yourLocalWeather");
        createNotification = true;
    }
    if (createNotification) {
        NotificationChannel channel = new NotificationChannel("yourLocalWeather",
                context.getString(R.string.notification_channel_name),
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(context.getString(R.string.notification_channel_description));
        channel.setVibrationPattern(isVibrateEnabled(context));
        channel.enableVibration(AppPreference.isVibrateEnabled(context));
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 2
Source File: NotificationChannels.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @return The vibrate setting for a specific recipient. If that recipient has no channel, this
 *         will return the setting for the default message channel.
 */
public static synchronized boolean getMessageVibrate(@NonNull Context context, @NonNull Recipient recipient) {
  if (!supported()) {
    return getMessageVibrate(context);
  }

  NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
  NotificationChannel channel             = notificationManager.getNotificationChannel(recipient.getNotificationChannel());

  if (!channelExists(channel)) {
    Log.w(TAG, "Recipient didn't have a channel. Returning message default.");
    return getMessageVibrate(context);
  }

  return channel.shouldVibrate();
}
 
Example 3
Source File: SlideshowService.java    From cwac-presentation with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
  handler=new Handler(Looper.getMainLooper());
  super.onCreate();

  NotificationManager mgr=
    (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O &&
    mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
    mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
      "Whatever", NotificationManager.IMPORTANCE_DEFAULT));
  }

  startForeground(1338, buildForegroundNotification());
}
 
Example 4
Source File: NotiHelper.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
public static void initNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Context context = HiApplication.getAppContext();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            int color = ContextCompat.getColor(context, R.color.icon_blue);
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(color);
            notificationChannel.enableVibration(false);
            notificationChannel.setBypassDnd(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
}
 
Example 5
Source File: ForegroundService.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
public static Notification createNotification(Context c) {
    NotificationManager nm = c.getSystemService(NotificationManager.class);

    String channelId = "foreground";
    if (nm.getNotificationChannel(channelId) == null) {
        nm.createNotificationChannel(new NotificationChannel(channelId, c.getString(R.string.appName), NotificationManager.IMPORTANCE_MIN));
    }

    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, c.getPackageName())
            .putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
    PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(c, channelId);
    builder.setContentIntent(pendingIntent);
    builder.setSmallIcon(R.drawable.ic_abicon);
    builder.setContentText(c.getString(R.string.clickToDisableNotification));
    builder.setPriority(NotificationCompat.PRIORITY_MIN);
    builder.setWhen(0); //show as last
    return builder.build();
}
 
Example 6
Source File: ForegroundService.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
public static Notification createNotification(Context c) {
    NotificationManager nm = c.getSystemService(NotificationManager.class);

    String channelId = "foreground";
    if (nm.getNotificationChannel(channelId) == null) {
        nm.createNotificationChannel(new NotificationChannel(channelId, c.getString(R.string.appName), NotificationManager.IMPORTANCE_MIN));
    }

    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, c.getPackageName())
            .putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
    PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(c, channelId);
    builder.setContentIntent(pendingIntent);
    builder.setSmallIcon(R.drawable.ic_abicon);
    builder.setContentText(c.getString(R.string.clickToDisableNotification));
    builder.setPriority(NotificationCompat.PRIORITY_MIN);
    builder.setWhen(0); //show as last
    return builder.build();
}
 
Example 7
Source File: BaseService.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * Android 8以降用にNotificationChannelを生成する処理
 * NotificationManager#getNotificationChannelがnullを
 * 返したときのみ新規に作成する
 * #createNotificationrから呼ばれる
 * showNotification
 * 	-> createNotificationBuilder
 * 		-> (createNotificationChannel
 * 			-> (createNotificationChannelGroup)
 * 			-> setupNotificationChannel)
 * 	-> createNotification
 * 	-> startForeground -> NotificationManager#notify
 * @param context
 * @return
 */
@TargetApi(Build.VERSION_CODES.O)
protected void createNotificationChannel(
	@NonNull final Context context) {
	
	final NotificationManager manager
		= ContextUtils.requireSystemService(context, NotificationManager.class);
	if (manager.getNotificationChannel(channelId) == null) {
		final NotificationChannel channel
			= new NotificationChannel(channelId, channelTitle, importance);
		if (!TextUtils.isEmpty(groupId)) {
			createNotificationChannelGroup(context, groupId, groupName);
			channel.setGroup(groupId);
		}
		channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
		manager.createNotificationChannel(setupNotificationChannel(channel));
	}
}
 
Example 8
Source File: NotificationReceiver.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 6 votes vote down vote up
private void updateNotificationChannelIfNeeded(Context context) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || notificationManager == null) {
        return;
    }

    NotificationChannel channel = notificationManager.getNotificationChannel(context.getString(R.string.live_streamer_notification_id));
    if (new Settings(context).getNotificationsSound()) {

        channel.setSound(
                RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
        );
    } else {
        channel.setSound(null, null);
    }
}
 
Example 9
Source File: NotificationChannels.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Updates the name of an existing channel to match the recipient's current name. Will have no
 * effect if the recipient doesn't have an existing valid channel.
 */
public static synchronized void updateContactChannelName(@NonNull Context context, @NonNull Recipient recipient) {
  if (!supported() || recipient.getNotificationChannel() == null) {
    return;
  }
  Log.i(TAG, "Updating contact channel name");

  NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);

  if (notificationManager.getNotificationChannel(recipient.getNotificationChannel()) == null) {
    Log.w(TAG, "Tried to update the name of a channel, but that channel doesn't exist.");
    return;
  }

  NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(),
                                                        recipient.getDisplayName(context),
                                                        NotificationManager.IMPORTANCE_HIGH);
  channel.setGroup(CATEGORY_MESSAGES);
  notificationManager.createNotificationChannel(channel);
}
 
Example 10
Source File: NotificationActivity.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private void ensureChannel(@NonNull NotificationManager manager) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return;
    }

    final NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
    if (channel == null) {
        manager.createNotificationChannel(new NotificationChannel(
                CHANNEL_ID,
                CHANNEL_ID,
                NotificationManager.IMPORTANCE_DEFAULT));
    }
}
 
Example 11
Source File: NotificationController.java    From MiPushFramework with GNU General Public License v3.0 5 votes vote down vote up
public static NotificationChannel registerChannelIfNeeded(Context context, String packageName) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return null;
    }

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = getChannelIdByPkg(packageName);
    NotificationChannel notificationChannel = manager.getNotificationChannel(channelId);

    if (notificationChannel != null) {
        if (ID_GROUP_APPLICATIONS.equals(notificationChannel.getGroup()) || TextUtils.isEmpty(notificationChannel.getGroup())) {
            manager.deleteNotificationChannel(channelId);
            notificationChannel = null;
        }
    }

    if (notificationChannel == null) {

        CharSequence name = ApplicationNameCache.getInstance().getAppName(context, packageName);
        if (name == null) {
            return null;
        }

        NotificationChannelGroup notificationChannelGroup = createGroupWithPackage(packageName, name);
        manager.createNotificationChannelGroup(notificationChannelGroup);

        notificationChannel = createChannelWithPackage(packageName, name);
        notificationChannel.setGroup(notificationChannelGroup.getId());

        manager.createNotificationChannel(notificationChannel);
    }

    return notificationChannel;

}
 
Example 12
Source File: NotificationBuilderO.java    From YalpStore with GNU General Public License v2.0 5 votes vote down vote up
public void initChannel() {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = manager.getNotificationChannel(BuildConfig.APPLICATION_ID);
    if (null == channel) {
        manager.createNotificationChannel(new NotificationChannel(
            BuildConfig.APPLICATION_ID,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_DEFAULT
        ));
    }
}
 
Example 13
Source File: NotificationBuilderO.java    From SmsScheduler with GNU General Public License v2.0 5 votes vote down vote up
public NotificationBuilderO(Context context) {
    super(context);
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = manager.getNotificationChannel(BuildConfig.APPLICATION_ID);
    if (null == channel) {
        manager.createNotificationChannel(new NotificationChannel(
                BuildConfig.APPLICATION_ID,
                context.getString(R.string.app_name),
                NotificationManager.IMPORTANCE_DEFAULT
        ));
    }
    builder.setChannelId(BuildConfig.APPLICATION_ID);
}
 
Example 14
Source File: ChannelBuilder.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 既存のNotificationChannelが存在すればその設定をコピーしてChannelBuilderを生成する。
 * 既存のNotificationChannelがなければ新規生成する。
 * @param context
 * @param channelId 通知チャネルid
 * @return
 */
@NonNull
public static ChannelBuilder getBuilder(@NonNull final Context context,
	@NonNull final String channelId) {
	
	if (DEBUG) Log.v(TAG, "getBuilder:" + channelId);
	final NotificationManager manager
		= ContextUtils.requireSystemService(context, NotificationManager.class);
	final NotificationChannel channel = manager.getNotificationChannel(channelId);
	if (channel != null) {
		// 既にNotificationChannelが存在する場合はその設定を取得して生成
		final ChannelBuilder builder = new ChannelBuilder(context,
			channelId, channel.getName(), channel.getImportance());
		builder.setLockscreenVisibility(channel.getLockscreenVisibility())
			.setBypassDnd(channel.canBypassDnd())
			.setShowBadge(channel.canShowBadge())
			.setDescription(channel.getDescription())
			.setLightColor(channel.getLightColor())
			.setVibrationPattern(channel.getVibrationPattern())
			.enableLights(channel.shouldShowLights())
			.enableVibration(channel.shouldVibrate())
			.setSound(channel.getSound(), channel.getAudioAttributes())
			.setGroup(channel.getGroup(), null)
			.setCreateIfExists(true);
		return builder;
	} else {
		// 存在しない場合は新規に生成
		return new ChannelBuilder(context,
			channelId, null, NotificationManager.IMPORTANCE_NONE);
	}
}
 
Example 15
Source File: BulkReadCardsService.java    From Walrus with GNU General Public License v3.0 5 votes vote down vote up
private Notification getNotification() {
    final NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (notificationManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
            && notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
        notificationManager.createNotificationChannel(
                new NotificationChannel(CHANNEL_ID,
                        getString(R.string.bulk_read_notification_channel_name),
                        NotificationManager.IMPORTANCE_LOW));
    }

    notificationBuilder
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getResources().getQuantityString(R.plurals.bulk_reading_from,
                    runners.size(), runners.size()))
            .setOngoing(true)
            .setProgress(0, 0, true)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setContentIntent(TaskStackBuilder.create(this)
                    .addNextIntentWithParentStack(new Intent(this, BulkReadCardsActivity.class))
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setCategory(Notification.CATEGORY_SERVICE);
    }

    return notificationBuilder.build();
}
 
Example 16
Source File: NotificationManagerHelper.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
private static void createChannelIfNotExist(NotificationManager notificationManager) {
    if (notificationManager.getNotificationChannel(CHANNEL_ONE_ID) == null) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                CHANNEL_ONE_NAME, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(R.color.accent);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}
 
Example 17
Source File: NotificationChannels.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized @Nullable Uri getMessageRingtone(@NonNull Context context, @NonNull Recipient recipient) {
  if (!supported() || recipient.resolve().getNotificationChannel() == null) {
    return null;
  }

  NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
  NotificationChannel channel             = notificationManager.getNotificationChannel(recipient.getNotificationChannel());

  if (!channelExists(channel)) {
    Log.w(TAG, "Recipient had no channel. Returning null.");
    return null;
  }

  return channel.getSound();
}
 
Example 18
Source File: FIRMessagingModule.java    From react-native-fcm with MIT License 4 votes vote down vote up
@ReactMethod
public void createNotificationChannel(ReadableMap details, Promise promise){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mngr = (NotificationManager) getReactApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        String id = details.getString("id");
        String name = details.getString("name");
        String priority = details.getString("priority");
        int importance;
        switch(priority) {
            case "min":
                importance = NotificationManager.IMPORTANCE_MIN;
                break;
            case "low":
                importance = NotificationManager.IMPORTANCE_LOW;
                break;
            case "high":
                importance = NotificationManager.IMPORTANCE_HIGH;
                break;
            case "max":
                importance = NotificationManager.IMPORTANCE_MAX;
                break;
            default:
                importance = NotificationManager.IMPORTANCE_DEFAULT;
        }
        if (mngr.getNotificationChannel(id) != null) {
            promise.resolve(null);
            return;
        }
        //
        NotificationChannel channel = new NotificationChannel(
                id,
                name,
                importance);
        // Configure the notification channel.
        if(details.hasKey("description")){
            channel.setDescription(details.getString("description"));
        }
        mngr.createNotificationChannel(channel);
    }
    promise.resolve(null);
}
 
Example 19
Source File: NewMessageChecker.java    From RedReader with GNU General Public License v3.0 4 votes vote down vote up
private static void createNotification(final String title, final String text, final Context context) {

		final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

		synchronized(sChannelCreated) {

			if(!sChannelCreated.getAndSet(true)) {

				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

					if(nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {

						Log.i(TAG, "Creating notification channel");

						final NotificationChannel channel = new NotificationChannel(
								NOTIFICATION_CHANNEL_ID,
								context.getString(R.string.notification_channel_name_reddit_messages),
								NotificationManager.IMPORTANCE_DEFAULT);

						nm.createNotificationChannel(channel);

					} else {
						Log.i(TAG, "Not creating notification channel as it already exists");
					}

				} else {
					Log.i(TAG, "Not creating notification channel due to old Android version");
				}
			}
		}

		final NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
				.setSmallIcon(R.drawable.icon_notif)
				.setContentTitle(title)
				.setContentText(text)
				.setAutoCancel(true)
				.setChannelId(NOTIFICATION_CHANNEL_ID);

		final Intent intent = new Intent(context, InboxListingActivity.class);
		notification.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));

		nm.notify(0, notification.getNotification());
	}
 
Example 20
Source File: PushTemplateHelper.java    From Android-SDK with MIT License 4 votes vote down vote up
static public NotificationChannel getNotificationChannel( final Context context, final String templateName )
{
  final String channelId = Backendless.getApplicationId() + ":" + templateName;
  NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
  return notificationManager.getNotificationChannel( channelId );
}