Android Firebase: как я могу создать канал уведомлений, который работает с моей существующей системой уведомлений - PullRequest
1 голос
/ 09 апреля 2020

Благодаря обучающей программе на испанском языке sh YouTube мне удалось создать систему уведомлений на основе Google Firebase. Но на данный момент уведомления отображаются только на устройствах с Android 7 и старше.

Это код, который я создал:

public class MiFirebaseMessagingService extends FirebaseMessagingService{

    public static final String TAG = "NOTICIAS";

    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String refreshedToken) {
    }

    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String from = remoteMessage.getFrom();
        Log.d(TAG, "Mensaje recible de: " + from);

        if( remoteMessage.getNotification() != null) {
            Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody());
        }

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Data: " + remoteMessage.getData());
        }

    }

После некоторых исследований я обнаружил, что с Android O необходим канал уведомлений.

С помощью После я уже пытался реализовать код для работы канала, но, к сожалению, безуспешно. Может кто-нибудь помочь, пожалуйста ??


    public class NotificationUtils extends ContextWrapper {

        private NotificationManager mManager;
        public static final String ANDROID_CHANNEL_ID = "mediengruppe.nova.notification.ANDROID";
        public static final String ANDROID_CHANNEL_NAME = "ANDROID CHANNEL";

        @RequiresApi(api = Build.VERSION_CODES.O)
        public NotificationUtils(Context base) {
            super(base);
            createChannels();
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        public void createChannels() {

            // create android channel
            NotificationChannel androidChannel = new NotificationChannel(ANDROID_CHANNEL_ID,
                    ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            // Sets whether notifications posted to this channel should display notification lights
            androidChannel.enableLights(true);
            // Sets whether notification posted to this channel should vibrate.
            androidChannel.enableVibration(true);

            // Sets whether notifications posted to this channel appear on the lockscreen or not
            androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

            getManager().createNotificationChannel(androidChannel);


        }

        private NotificationManager getManager() {
            if (mManager == null) {
                mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
            return mManager;
        }
    }

}
...