首页 > 解决方案 > Android Firebase:如何创建适用于我现有通知系统的通知通道

问题描述

感谢西班牙语 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;
        }
    }

}

标签: androidfirebasepush-notificationchannelandroid-8.0-oreo

解决方案


推荐阅读