android.app.NotificationManager#getNotificationChannel ( )源码实例Demo

下面列出了android.app.NotificationManager#getNotificationChannel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: your-local-weather   文件: NotificationUtils.java
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);
    }
}
 
源代码2 项目: mollyim-android   文件: NotificationChannels.java
/**
 * @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();
}
 
源代码3 项目: mollyim-android   文件: NotificationChannels.java
/**
 * 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);
}
 
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);
    }
}
 
源代码5 项目: libcommon   文件: BaseService.java
/**
 * 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));
	}
}
 
源代码6 项目: prayer-times-android   文件: ForegroundService.java
@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();
}
 
源代码7 项目: prayer-times-android   文件: ForegroundService.java
@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();
}
 
源代码8 项目: hipda   文件: NotiHelper.java
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);
        }
    }
}
 
源代码9 项目: cwac-presentation   文件: SlideshowService.java
@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());
}
 
源代码10 项目: mollyim-android   文件: NotificationChannels.java
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();
}
 
源代码11 项目: Walrus   文件: BulkReadCardsService.java
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();
}
 
源代码12 项目: Markwon   文件: NotificationActivity.java
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));
    }
}
 
源代码13 项目: MiPushFramework   文件: NotificationController.java
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;

}
 
源代码14 项目: YalpStore   文件: NotificationBuilderO.java
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
        ));
    }
}
 
源代码15 项目: SmsScheduler   文件: NotificationBuilderO.java
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);
}
 
源代码16 项目: libcommon   文件: ChannelBuilder.java
/**
 * 既存の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);
	}
}
 
@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);
    }
}
 
源代码18 项目: RedReader   文件: NewMessageChecker.java
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());
	}
 
源代码19 项目: react-native-fcm   文件: FIRMessagingModule.java
@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);
}
 
源代码20 项目: Android-SDK   文件: PushTemplateHelper.java
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 );
}