Firebase notificacion sound / vibration when app in background with Android Oreo

随声附和 提交于 2019-12-11 17:00:26

问题


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

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

This is my code:

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());

}

回答1:


i found this in the documentation. May be it will help you :

  • 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 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. Individual notifications must now be put in a specific channel.

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

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

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

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.



来源:https://stackoverflow.com/questions/52835585/firebase-notificacion-sound-vibration-when-app-in-background-with-android-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!