// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.browser.examples.twa_notification_delegation;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;

import com.google.androidbrowserhelper.trusted.DelegationService;

public class NotificationDelegationService extends DelegationService {
    @Override
    public boolean onNotifyNotificationWithChannel(
            @NonNull String platformTag,
            int platformId,
            @NonNull Notification notification,
            @NonNull String channelName) {
        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                this.getPackageName() + "/" + R.raw.airhorn);

        // Notification.Builder.recoverBuilder() was introduce in Nougat, so we prefer it when
        // available.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Notification.Builder builder =
                    Notification.Builder.recoverBuilder(this, notification);

            // From Android O and above, the sound is set on the Channel instad of the notification.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder.setChannelId(channelName);
                NotificationChannel channel = new NotificationChannel(
                        channelName, channelName, NotificationManager.IMPORTANCE_HIGH);
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                channel.setSound(alarmSound, audioAttributes);
                mNotificationManager.createNotificationChannel(channel);
            }

            builder.setSound(alarmSound);
            notification = builder.build();
        } else {
            notification.sound = alarmSound;
        }

        mNotificationManager.notify(platformTag, platformId, notification);
        return true;
    }
}

@@ -2031,16 +2031,17 @@
 nel inst
+e
 ad of th

Update demos/twa-notification-delegation/src/main/java/com/google/browser/examples/twa_notification_delegation/NotificationDelegationService.java