【问题标题】:NotificationCompat with API 26NotificationCompat 与 API 26
【发布时间】:2017-06-08 19:03:35
【问题描述】:

我没有看到任何关于如何将 NotificationCompat 与 Android O 的 Notification Channels 一起使用的信息

我确实看到了一个采用 channelId 的新构造函数,但是如何获取 Compat 通知并在 NotificationChannel 中使用它,因为 createNotificationChannel 采用 NotificationChannel 对象

【问题讨论】:

标签: android android-notifications


【解决方案1】:

仅当 API >= 26 时创建 NotificationChannel

public void initChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("default",
                                                          "Channel name",
                                                          NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");
    notificationManager.createNotificationChannel(channel);
}

然后只需使用:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default");

因此,您的通知适用于 API 26(带通道)和更低版本(不带)。

【讨论】:

  • 您不必设置声音/灯光/振动,您可以做到。例如,如果您有一个带有自定义声音/灯光/振动和通知自定义振动的频道,不确定会发生什么。使用 API 26,因为在此之后通道将被忽略
  • @stankocken 最近有变化 - 我的 NotificationCompat.Builder 只有 setChannel,没有 NotificationCompat.Builder.setChannelId,也无法将通道 ID 传递给构造函数。我正在使用 support.v4。我可以看到在设置中创建了频道,但 setChannel 似乎不起作用,因为调试抱怨频道为空(所以不发送)
  • @PaulHadfield 在文档中你仍然有setChannelId 并且构造函数也接受了它。 developer.android.com/reference/android/support/v4/app/…
  • 频道是需要创建一次,还是需要每次都创建?
  • 我认为你只需要创建一次。但是如果你多次创建它,它只会覆盖以前的,所以不是一个大问题 IMO
【解决方案2】:

声明通知管理器:

   final NotificationManager mNotific=            
   (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    CharSequence name="Ragav";
    String desc="this is notific";
    int imp=NotificationManager.IMPORTANCE_HIGH;
    final String ChannelID="my_channel_01";

通知渠道

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
    {
      NotificationChannel mChannel = new NotificationChannel(ChannelID, name, 
     imp);
            mChannel.setDescription(desc);
            mChannel.setLightColor(Color.CYAN);
            mChannel.canShowBadge();
            mChannel.setShowBadge(true);
            mNotific.createNotificationChannel(mChannel);
        }

    final int ncode=101;

    String Body="This is testing notific";

通知生成器

        Notification n= new Notification.Builder(this,ChannelID)
                .setContentTitle(getPackageName())
                .setContentText(Body)
                .setBadgeIconType(R.mipmap.ic_launcher)
                .setNumber(5)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .build();

NotificationManager 通知用户:

            mNotific.notify(ncode, n);

【讨论】:

    【解决方案3】:

    NotificationChannel 实际上将多个通知分组到频道中。它基本上为用户提供了对通知行为的更多控制。您可以在Working with Notification Channel | With Example

    阅读有关通知通道及其实现的更多信息

    通知通道仅适用于 Android Oreo。

     //Notification channel should only be created for devices running Android 26
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
      NotificationChannel notificationChannel = new NotificationChannel("unique_channel_id","channel_name",NotificationManager.IMPORTANCE_DEFAULT);
    
      //Boolean value to set if lights are enabled for Notifications from this Channel
      notificationChannel.enableLights(true);
    
      //Boolean value to set if vibration is enabled for Notifications from this Channel
      notificationChannel.enableVibration(true);
    
      //Sets the color of Notification Light
      notificationChannel.setLightColor(Color.GREEN);
    
      //Set the vibration pattern for notifications. Pattern is in milliseconds with the format {delay,play,sleep,play,sleep...}
      notificationChannel.setVibrationPattern(new long[]{500,500,500,500,500});
    
      //Sets whether notifications from these Channel should be visible on Lockscreen or not
      notificationChannel.setLockscreenVisibility( Notification.VISIBILITY_PUBLIC);
    }  
    

    请注意,传递给构造函数的通道 ID 充当该通知通道的唯一标识符。现在创建如下所示的通知

    // Creating the Channel
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);
    

    要向该频道添加任何通知,只需传递频道 ID,如下所示

    //We pass the unique channel id as the second parameter in the constructor
    NotificationCompat.Builder notificationCompatBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
    
    //Title for your notification
    notificationCompatBuilder.setContentTitle("This is title");
    
    //Subtext for your notification
    notificationCompatBuilder.setContentText("This is subtext");
    
    //Small Icon for your notificatiom
    notificationCompatBuilder.setSmallIcon(R.id.icon);
    
    //Large Icon for your notification 
    notificationCompatBuilder.setLargeIcon(  BitmapFactory.decodeResource(getResources(),R.id.icon));
    
    notificationManager.notify( NOTIFICATION_ID,notificationCompatBuilder.build());
    

    【讨论】:

    • 将通道 ID 添加到构建器构造函数对我来说失败:错误:类 Builder 中的构造函数 Builder 不能应用于给定类型; new NotificationCompat.Builder(mContext, "my_channel_01") ^ required: Context found: Context,String 原因:实际参数列表和正式参数列表的长度不同
    【解决方案4】:

    如果您完成了所有工作但没有得到任何结果,请小心。在某些设备上,您必须设置通知priority

       final NotificationCompat.Builder mBuilder = new 
        NotificationCompat.Builder(mContext, "default")
        .setPriority(Notification.PRIORITY_MAX);
    

    【讨论】:

      【解决方案5】:

      我知道这个答案迟了,但迟到总比不做好!
      我刚刚发布了notification-channel-compat 库,它提供了可追溯到 OS 4.0 的通知通道支持。由于开发人员无论如何都必须针对 Channels 进行设计,因此他们现在可以在所有设备上使用 Channels 的优势,而不必为旧设备单独设计。
      该库使用 OS 8.0+ 设备的内置通道类,并为旧设备模仿它。只需使用我们的NotificationChannelCompatNotificationChannelGroupCompatNotificationChannelManagerHelper 类,并添加一行代码。您可以在github 看到更多信息。请测试它并让我知道任何issues
      谢谢,
      抄写员

      【讨论】:

        猜你喜欢
        • 2019-10-18
        • 2015-02-17
        • 1970-01-01
        • 2021-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多