使用Android Oreo在后台运行应用程序时Firebase发出通知的声音/振动 [英] Firebase notificacion sound / vibration when app in background with Android Oreo

查看:130
本文介绍了使用Android Oreo在后台运行应用程序时Firebase发出通知的声音/振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Android Oreo在后台运行应用程序时,我听不到声音或振动.在前景中使用应用程序时,声音和振动效果很好.

I can't get sound or vibration when app in background with Android Oreo. With app in foreground sound and vibration works fine.

使用早期版本的android且没有Nofication Channel时,一切正常,但现在我找不到实现此目的的方法.

With previous versions of android and without Nofication Channel everything worked fine but now I don't find the way to do this.

这是我的代码:

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 8.0(API级别26)及更高版本上,通知的重要性 由通知所针对的频道的重要性决定 发布到.用户可以更改通知渠道的重要性 在系统设置中(图12).在Android 7.1(API级别25)和 下面,每个通知的重要性由 通知的优先级.

On Android 8.0 (API level 26) and above, importance of a notification is determined by the importance of the channel the notification was posted to. Users can change the importance of a notification channel in the system settings (figure 12). On Android 7.1 (API level 25) and below, importance of each notification is determined by the notification's priority.

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

    Android O introduces notification channels to provide a unified system to help users manage notifications. When you target Android O, you must implement one or more notification channels to display notifications to your users. If you don't target Android O, your apps behave the same as they do on Android 7.0 when running on Android O devices.

  • 1 .现在必须将各个通知放在特定的频道中.

    1. Individual notifications must now be put in a specific channel.

    2 .用户现在可以关闭每个频道的通知,而不必关闭 关闭来自应用程序的所有通知.

    2. Users can now turn off notifications per channel, instead of turning off all notifications from an app.

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

    3. Apps with active notifications display a notification "badge" on top of their app icon on the home/launcher screen.

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

    4. Users can now snooze a notification from the drawer. You can set an automatic timeout for a notification.

    5 .一些与通知行为有关的API已从 通知到NotificationChannel.例如使用 NotificationChannel.setImportance()代替 适用于Android 8.0及更高版本的NotificationCompat.Builder.setPriority().

    5. Some APIs regarding notification behaviors were moved from Notification to NotificationChannel. For example, use NotificationChannel.setImportance() instead of NotificationCompat.Builder.setPriority() for Android 8.0 and higher.

    这篇关于使用Android Oreo在后台运行应用程序时Firebase发出通知的声音/振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆