【问题标题】:Firebase notificacion sound / vibration when app in background with Android Oreo应用程序在后台使用 Android Oreo 时的 Firebase 通知声音/振动
【发布时间】:2019-03-20 23:51:31
【问题描述】:

当应用在后台运行 Android Oreo 时,我无法听到声音或振动。应用在前台声音和振动工作正常。

使用以前版本的 android 并且没有通知频道一切正常,但现在我找不到这样做的方法。

这是我的代码:

private void showNotification(String title, String body) {

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.name.notification.test";

    Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.my_sound);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build();

        notificationChannel.setDescription("EDMT Channel");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});           
        notificationChannel.setSound(sound, audioAttributes); // This is IMPORTANT            
        notificationManager.createNotificationChannel(notificationChannel);

    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( this,NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(body)
            .setContentInfo("Info");

    notificationManager.notify(new Random().nextInt(),notificationBuilder.build());

}

【问题讨论】:

标签: android firebase firebase-cloud-messaging android-8.0-oreo


【解决方案1】:

我在文档中找到了这个。也许它会帮助你:

  • 在 Android 8.0(API 级别 26)及更高版本上,通知的重要性 由通知的渠道的重要性决定 发布到。用户可以更改通知渠道的重要性 在系统设置中(图 12)。在 Android 7.1(API 级别 25)和 下面,每个通知的重要性由 通知的优先级。

  • Android O 引入通知通道以提供统一的系统 帮助用户管理通知。当您以 Android O 为目标时,您 必须实现一个或多个通知通道才能显示 通知您的用户。如果您不针对 Android O,您的应用 在 Android O 上运行时的行为与在 Android 7.0 上的行为相同 设备。

1。现在必须将单独的通知放在特定的频道中。

2。用户现在可以关闭每个频道的通知,而不是关闭 关闭应用的所有通知。

3。具有活动通知的应用程序在顶部显示通知“徽章” 在主屏幕/启动器屏幕上显示他们的应用图标。

4。用户现在可以暂停抽屉中的通知。您可以设置一个 通知自动超时。

5。一些关于通知行为的 API 已从 通知 NotificationChannel。例如,使用 NotificationChannel.setImportance() 而不是 NotificationCompat.Builder.setPriority() 适用于 Android 8.0 及更高版本。

【讨论】:

  • 放入文档的实际链接会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多