Java Code Examples for android.app.NotificationChannel#setVibrationPattern()

The following examples show how to use android.app.NotificationChannel#setVibrationPattern() . 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: CompatibilityDemoInit.java    From ans-android-sdk with GNU General Public License v3.0 6 votes vote down vote up
private static void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道的id
        String id = "1";
        // 用户可以看到的通知渠道的名字.
        CharSequence name = "notification channel";
        // 用户可以看到的通知渠道的描述
        String description = "notification description";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);
        // 配置通知渠道的属性
        mChannel.setDescription(description);
        // 设置通知出现时的闪灯(如果 android 设备支持的话)
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        // 设置通知出现时的震动(如果 android 设备支持的话)
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        //最后在notificationmanager中创建该通知渠道
        mNotificationManager.createNotificationChannel(mChannel);
    }
}
 
Example 3
Source File: MaintenanceNotificationChannel.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
public static void init(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String name = "Technical maintenance";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(ID, name, importance);
        channel.setSound(null, null);
        channel.setVibrationPattern(null);

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);

        // Mandatory; it is not possible to change the sound of an existing channel after its initial creation
        Objects.requireNonNull(notificationManager, "notificationManager must not be null");
        notificationManager.deleteNotificationChannel(ID_OLD);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 4
Source File: NotificationUtils.java    From YCNotification with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
    //第一个参数:channel_id
    //第二个参数:channel_name
    //第三个参数:设置通知重要性级别
    //注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
    //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.canBypassDnd();//是否绕过请勿打扰模式
    channel.enableLights(true);//闪光灯
    channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
    channel.setLightColor(Color.RED);//闪关灯的灯光颜色
    channel.canShowBadge();//桌面launcher的消息角标
    channel.enableVibration(true);//是否允许震动
    channel.getAudioAttributes();//获取系统通知响铃声音的配置
    channel.getGroup();//获取通知取到组
    channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
    channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
    channel.shouldShowLights();//是否会有灯光
    getManager().createNotificationChannel(channel);
}
 
Example 5
Source File: NotificationChannels.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
private synchronized void createNotificationChannel() {
    CharSequence name = MobiComKitConstants.PUSH_NOTIFICATION_NAME;
    int importance = NotificationManager.IMPORTANCE_HIGH;

    if (mNotificationManager != null && mNotificationManager.getNotificationChannel(MobiComKitConstants.AL_PUSH_NOTIFICATION) == null) {
        NotificationChannel mChannel = new NotificationChannel(MobiComKitConstants.AL_PUSH_NOTIFICATION, name, importance);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.GREEN);
        mChannel.setShowBadge(ApplozicClient.getInstance(context).isUnreadCountBadgeEnabled());

        if (ApplozicClient.getInstance(context).getVibrationOnNotification()) {
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        }

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
        mChannel.setSound(TextUtils.isEmpty(soundFilePath) ? RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) : Uri.parse(soundFilePath), audioAttributes);
        mNotificationManager.createNotificationChannel(mChannel);
        Utils.printLog(context, TAG, "Created notification channel");
    }
}
 
Example 6
Source File: BulkDownloadService.java    From odyssey with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Opens a notification channel and disables the LED and vibration
 */
private void openChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.getResources().getString(R.string.notification_channel_downloader), android.app.NotificationManager.IMPORTANCE_LOW);
        // Disable lights & vibration
        channel.enableVibration(false);
        channel.enableLights(false);
        channel.setVibrationPattern(null);

        // Allow lockscreen control
        channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        // Register the channel
        mNotificationManager.createNotificationChannel(channel);
    }
}
 
Example 7
Source File: SmbService.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //创建NotificationChannel
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel channel = new NotificationChannel("com.xyoye.dandanplay.smbservice.playchannel", "SMB服务", NotificationManager.IMPORTANCE_LOW);
        channel.enableVibration(false);
        channel.setVibrationPattern(new long[]{0});
        channel.enableLights(false);
        channel.setSound(null, null);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
    startForeground(NOTIFICATION_ID, buildNotification());
    return super.onStartCommand(intent, flags, startId);
}
 
Example 8
Source File: MainActivity.java    From BroadCastReceiver with Apache License 2.0 6 votes vote down vote up
/**
 * for API 26+ create notification channels
 */
private void createchannel() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = new NotificationChannel(id,
            getString(R.string.channel_name),  //name of the channel
            NotificationManager.IMPORTANCE_DEFAULT);   //importance level
        //important level: default is is high on the phone.  high is urgent on the phone.  low is medium, so none is low?
        // Configure the notification channel.
        mChannel.setDescription(getString(R.string.channel_description));
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setShowBadge(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        nm.createNotificationChannel(mChannel);

    }
}
 
Example 9
Source File: NotificationManager.java    From weMessage with GNU Affero General Public License v3.0 6 votes vote down vote up
private void initialize(){
    android.app.NotificationManager notificationManager = (android.app.NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(weMessage.NOTIFICATION_CHANNEL_NAME) == null) {
        NotificationChannel channel = new NotificationChannel(weMessage.NOTIFICATION_CHANNEL_NAME, app.getString(R.string.notification_channel_name), android.app.NotificationManager.IMPORTANCE_HIGH);
        AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build();

        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setLightColor(Color.BLUE);
        channel.setVibrationPattern(new long[]{1000, 1000});
        channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);

        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 10
Source File: NotifyAction.java    From opentasks with Apache License 2.0 6 votes vote down vote up
private static void createChannels(Context context)
{
    if (Build.VERSION.SDK_INT >= 26)
    {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel pinnedChannel = new NotificationChannel(ActionService.CHANNEL_PINNED,
                context.getString(R.string.opentasks_notification_channel_pinned_tasks),
                NotificationManager.IMPORTANCE_DEFAULT);
        // pinned Notifications should not get a badge
        pinnedChannel.setShowBadge(true);
        pinnedChannel.enableLights(false);
        pinnedChannel.enableVibration(true);
        pinnedChannel.setVibrationPattern(new long[] { 0, 100, 100, 100, 0 });
        pinnedChannel.setSound(null, null);
        nm.createNotificationChannel(pinnedChannel);

        NotificationChannel dueDates = new NotificationChannel(ActionService.CHANNEL_DUE_DATES,
                context.getString(R.string.opentasks_notification_channel_due_dates), NotificationManager.IMPORTANCE_HIGH);
        dueDates.setShowBadge(true);
        dueDates.enableLights(true);
        dueDates.enableVibration(true);
        nm.createNotificationChannel(dueDates);
    }
}
 
Example 11
Source File: MainActivity.java    From BroadCastReceiver with Apache License 2.0 6 votes vote down vote up
/**
 * for API 26+ create notification channels
 */
private void createchannel() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(id,
            getString(R.string.channel_name),  //name of the channel
            NotificationManager.IMPORTANCE_DEFAULT);   //importance level
        //important level: default is is high on the phone.  high is urgent on the phone.  low is medium, so none is low?
        // Configure the notification channel.
        mChannel.setDescription(getString(R.string.channel_description));
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setShowBadge(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        nm.createNotificationChannel(mChannel);

    }
}
 
Example 12
Source File: NotificationStatusBarWithCustomViewActivity.java    From coursera-android with MIT License 5 votes vote down vote up
private void createNotificationChannel() {

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mChannelID = getPackageName() + ".channel_01";
        // The user-visible name of the channel.
        CharSequence name = getString(R.string.channel_name);

        // The user-visible description of the channel.
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(mChannelID, name, importance);

        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);

        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(mVibratePattern);

        //Uri soundURI = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alarm_rooster);
        mChannel.setSound(soundURI, (new AudioAttributes.Builder()).setUsage(AudioAttributes.USAGE_NOTIFICATION).build());

        mNotificationManager.createNotificationChannel(mChannel);
    }
 
Example 13
Source File: PushTemplateHelper.java    From Android-SDK with MIT License 5 votes vote down vote up
static private NotificationChannel updateNotificationChannel( Context context, NotificationChannel notificationChannel, final AndroidPushTemplate template )
{
  if( template.getShowBadge() != null )
    notificationChannel.setShowBadge( template.getShowBadge() );

  if( template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6 )
    notificationChannel.setImportance( template.getPriority() ); // NotificationManager.IMPORTANCE_DEFAULT

  AudioAttributes audioAttributes = new AudioAttributes.Builder()
          .setUsage( AudioAttributes.USAGE_NOTIFICATION_RINGTONE )
          .setContentType( AudioAttributes.CONTENT_TYPE_SONIFICATION )
          .setFlags( AudioAttributes.FLAG_AUDIBILITY_ENFORCED )
          .setLegacyStreamType( AudioManager.STREAM_NOTIFICATION )
          .build();

  notificationChannel.setSound( PushTemplateHelper.getSoundUri( context, template.getSound() ), audioAttributes );

  if (template.getLightsColor() != null)
  {
    notificationChannel.enableLights( true );
    notificationChannel.setLightColor( template.getLightsColor()|0xFF000000 );
  }

  if( template.getVibrate() != null && template.getVibrate().length > 0 )
  {
    long[] vibrate = new long[ template.getVibrate().length ];
    int index = 0;
    for( long l : template.getVibrate() )
      vibrate[ index++ ] = l;

    notificationChannel.enableVibration( true );
    notificationChannel.setVibrationPattern( vibrate );
  }

  return notificationChannel;
}
 
Example 14
Source File: ScreenshotService.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
public ScreenshotService(Context ctx, BleDevice device)
{
    mDevice = device;
    mCtx = ctx;
    mNM = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Screenshot Service", NotificationManager.IMPORTANCE_MIN);
        notificationChannel.setDescription("Screenshot download");
        notificationChannel.setVibrationPattern(new long[]{0L});
        mNM.createNotificationChannel(notificationChannel);
    }
}
 
Example 15
Source File: NotificationBuilder.java    From beaconloc with Apache License 2.0 4 votes vote down vote up
/**
 * Creation of notification on operations completed
 */
public NotificationBuilder createNotification(String title, String ringtone, boolean vibrate, PendingIntent notifyIntent) {

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);


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

        mNotificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                title, NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mNotificationChannel.setDescription("beacon location notification channel");
        mNotificationChannel.enableLights(true);

        if (vibrate) {
            mNotificationChannel.setVibrationPattern(mVibrate_pattern);
            mNotificationChannel.enableVibration(true);
        } else {
            mNotificationChannel.enableVibration(false);
        }

        mNotificationChannel.setLightColor(Color.YELLOW);
        mNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        mNotificationChannel.setSound(null, null);

       /* if (ringtone != null) {
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();
            mNotificationChannel.setSound(Uri.parse(ringtone), att);
        } else {
            mNotificationChannel.setSound(null, null);
        }
        */
        notificationManager.createNotificationChannel(mNotificationChannel);
    }

    mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);

    mBuilder.setAutoCancel(true)
            .setDefaults(vibrate?(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE):Notification.DEFAULT_LIGHTS)
            .setSmallIcon(getNotificationSmallIcon())
            .setContentTitle(title)
            .setColor(ContextCompat.getColor(mContext, R.color.hn_orange));

    setRingtone(ringtone);

    if (notifyIntent != null) {
        mBuilder.setContentIntent(notifyIntent);
    }

    setLargeIcon(getNotificationLargeIcon());

    return this;

}
 
Example 16
Source File: SynchronizationService.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate() {
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Synchronization Service", NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setDescription("Connection status");
        notificationChannel.setVibrationPattern(new long[]{0L});
        notificationChannel.setShowBadge(false);
        mNM.createNotificationChannel(notificationChannel);
    }

    mBleMngr = get(getApplication());
    BleManagerConfig cfg = new BleManagerConfig();
    cfg.forceBondDialog = true;
    cfg.taskTimeoutRequestFilter = new TaskTimeoutRequestFilter();
    cfg.defaultScanFilter = new WatchesFilter();
    cfg.enableCrashResolver = true;
    cfg.bondFilter = new BondFilter();
    cfg.alwaysUseAutoConnect = true;
    cfg.useLeTransportForBonding = true;
    if (BuildConfig.DEBUG)
        cfg.loggingEnabled = true;
    mBleMngr.setConfig(cfg);

    mPrefs = getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
    String defaultDevMacAddr = mPrefs.getString(MainActivity.PREFS_DEFAULT_MAC_ADDR, "");
    String defaultLocalName = mPrefs.getString(MainActivity.PREFS_DEFAULT_LOC_NAME, "");

    if(!defaultDevMacAddr.isEmpty()) {
        if(!mBleMngr.hasDevice(defaultDevMacAddr))
            mBleMngr.newDevice(defaultDevMacAddr, defaultLocalName);

        mDevice = mBleMngr.getDevice(defaultDevMacAddr);
        mDevice.setListener_State(SynchronizationService.this);

        mWeatherService = new WeatherService(getApplicationContext(), mDevice);
        mNotificationService = new NotificationService(getApplicationContext(), mDevice);
        mMediaService = new MediaService(getApplicationContext(), mDevice);
        mScreenshotService = new ScreenshotService(getApplicationContext(), mDevice);
        mTimeService = new TimeService(getApplicationContext(), mDevice);
        silentModeService = new SilentModeService(getApplicationContext());

        mDevice.connect();
    }

    updateNotification();
}
 
Example 17
Source File: LeanplumNotificationChannel.java    From Leanplum-Android-SDK with Apache License 2.0 4 votes vote down vote up
/**
 * Create push notification channel with provided id, name and importance of the channel.
 * You can call this method also when you need to update the name or description of a channel, at
 * this case you should use original channel id.
 *
 * @param context The application context.
 * @param channelId The id of the channel.
 * @param channelName The user-visible name of the channel.
 * @param channelImportance The importance of the channel. Use value from 0 to 5. 3 is default.
 * Read more https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_DEFAULT
 * Once you create a notification channel, only the system can modify its importance.
 * @param channelDescription The user-visible description of the channel.
 * @param groupId The id of push notification channel group.
 * @param enableLights True if lights enable for this channel.
 * @param lightColor Light color for notifications posted to this channel, if the device supports
 * this feature.
 * @param enableVibration True if vibration enable for this channel.
 * @param vibrationPattern Vibration pattern for notifications posted to this channel.
 * @param lockscreenVisibility How to be shown on the lockscreen.
 * @param bypassDnd Whether should notification bypass DND.
 * @param showBadge Whether should notification show badge.
 */
private static void createNotificationChannel(Context context, String channelId, String
    channelName, int channelImportance, String channelDescription, String groupId, boolean
    enableLights, int lightColor, boolean enableVibration, long[] vibrationPattern, int
    lockscreenVisibility, boolean bypassDnd, boolean showBadge) {
  if (context == null || TextUtils.isEmpty(channelId)) {
    return;
  }
  if (BuildUtil.isNotificationChannelSupported(context)) {
    try {
      NotificationManager notificationManager =
          context.getSystemService(NotificationManager.class);
      if (notificationManager == null) {
        Log.e("Notification manager is null");
        return;
      }

      NotificationChannel notificationChannel = new NotificationChannel(channelId,
          channelName, channelImportance);
      if (!TextUtils.isEmpty(channelDescription)) {
        notificationChannel.setDescription(channelDescription);
      }
      if (enableLights) {
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(lightColor);
      }
      if (enableVibration) {
        notificationChannel.enableVibration(true);
        // Workaround for https://issuetracker.google.com/issues/63427588
        if (vibrationPattern != null && vibrationPattern.length != 0) {
          notificationChannel.setVibrationPattern(vibrationPattern);
        }
      }
      if (!TextUtils.isEmpty(groupId)) {
        notificationChannel.setGroup(groupId);
      }
      notificationChannel.setLockscreenVisibility(lockscreenVisibility);
      notificationChannel.setBypassDnd(bypassDnd);
      notificationChannel.setShowBadge(showBadge);

      notificationManager.createNotificationChannel(notificationChannel);
    } catch (Throwable t) {
      Util.handleException(t);
    }
  }
}
 
Example 18
Source File: NotificationChannels.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {

    final Notification temp = wip.build();
    if (temp.getChannelId() == null) return null;

    // create generic audio attributes
    final AudioAttributes generic_audio = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
            .build();

    // create notification channel for hashing purposes from the existing notification builder
    NotificationChannel template = new NotificationChannel(
            temp.getChannelId(),
            getString(temp.getChannelId()),
            NotificationManager.IMPORTANCE_DEFAULT);


    // mirror the notification parameters in the channel
    template.setGroup(temp.getChannelId());
    template.setVibrationPattern(temp.vibrate);
    template.setSound(temp.sound, generic_audio);
    template.setLightColor(temp.ledARGB);
    if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
        template.enableLights(true); // weird how this doesn't work like vibration pattern
    template.setDescription(temp.getChannelId() + " " + wip.hashCode());

    // get a nice string to identify the hash
    final String mhash = my_text_hash(template);

    // create another notification channel using the hash because id is immutable
    final NotificationChannel channel = new NotificationChannel(
            template.getId() + mhash,
            getString(temp.getChannelId()) + mhash,
            NotificationManager.IMPORTANCE_DEFAULT);

    // mirror the settings from the previous channel
    channel.setSound(template.getSound(), generic_audio);
    if (addChannelGroup()) {
        channel.setGroup(template.getGroup());
    } else {
        channel.setGroup(channel.getId());
    }
    channel.setDescription(template.getDescription());
    channel.setVibrationPattern(template.getVibrationPattern());
    template.setLightColor(temp.ledARGB);
    if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
        template.enableLights(true); // weird how this doesn't work like vibration pattern
    template.setDescription(temp.getChannelId() + " " + wip.hashCode());

    // create a group to hold this channel if one doesn't exist or update text
    getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
    // create this channel if it doesn't exist or update text
    getNotifManager().createNotificationChannel(channel);
    return channel;
}
 
Example 19
Source File: FirebaseMessagingPlugin.java    From cordova-plugin-firebase-messaging with MIT License 4 votes vote down vote up
@CordovaMethod
private void createChannel(JSONObject options, CallbackContext callbackContext) throws JSONException {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        throw new UnsupportedOperationException("Notification channels are not supported");
    }

    String channelId = options.getString("id");
    String channelName = options.getString("name");
    int importance = options.getInt("importance");
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    channel.setDescription(options.optString("description", ""));
    channel.setShowBadge(options.optBoolean("badge", true));

    channel.enableLights(options.optBoolean("light", false));
    channel.setLightColor(options.optInt("lightColor", 0));

    String soundName = options.optString("sound", "default");
    if (!"default".equals(soundName)) {
        String packageName = cordova.getActivity().getPackageName();
        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + soundName);
        channel.setSound(soundUri, new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build());
    }

    JSONArray vibrationPattern = options.optJSONArray("vibration");
    if (vibrationPattern != null) {
        int patternLength = vibrationPattern.length();
        long[] patternArray = new long[patternLength];
        for (int i = 0; i < patternLength; i++) {
            patternArray[i] = vibrationPattern.getLong(i);
        }
        channel.setVibrationPattern(patternArray);
        channel.enableVibration(true);
    } else {
        channel.enableVibration(options.optBoolean("vibration", true));
    }

    notificationManager.createNotificationChannel(channel);

    callbackContext.success();
}
 
Example 20
Source File: FloatViewManager.java    From RelaxFinger with GNU General Public License v2.0 2 votes vote down vote up
private void createShowNotification() {

        NotificationManager mNF = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder;

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

            String name = "com_hardwork_fg607_relaxfinger_channel";
            String id = "悬浮助手";
            String description = "悬浮助手channel";

            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mNF.createNotificationChannel(mChannel);

            mBuilder =
                    new NotificationCompat.Builder(mContext,id)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("RelaxFinger")
                            .setTicker("悬浮球隐藏到通知栏啦,点击显示!")
                            .setContentText("点击显示悬浮球");

        }else{

             mBuilder =
                    new NotificationCompat.Builder(mContext)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("RelaxFinger")
                            .setTicker("悬浮球隐藏到通知栏啦,点击显示!")
                            .setContentText("点击显示悬浮球");

        }

        Intent resultIntent = new Intent(Config.ACTION_SHOW_FLOATBALL);

        PendingIntent resultPendingIntent = PendingIntent.getBroadcast(mContext,(int) SystemClock.uptimeMillis(), resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);


        Notification notification = mBuilder.build();

        notification.flags |= Notification.FLAG_NO_CLEAR;

        mNF.notify(R.string.app_name, notification);

    }