android - 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;
}
}
}
解决方案
推荐阅读
- html - 表格中的图像根本没有边框?如何?宽度和高度问题.. 在 css / html 中
- node.js - NPM 安装在 docker 容器中失败 - npm WARN tar ENOENT:没有这样的文件或目录
- laravel - Vuejs发生变化时重新加载表的数据
- java - 在java中的长问题中移动最高有效位
- python - python flask简单的resetful api不仅仅是在sqlite数据库中添加1个记录用户
- html - 当鼠标移动到图片上时,HTML 会屏蔽图形
- c - 错误:(1089) 对“_forward_flight”XC8 编译器的递归函数调用
- c - bfSize 问题(Pset3、任务调整大小、CS50)
- lambda - 闭包作为 C++ 中的参数和返回类型
- css - 在 Firefox 上,CSS 网格右侧的 HTML 输入溢出