android - 通知不显示 Firebase Android 8
问题描述
我有一个应该显示推送通知的应用程序,但它没有new NotificationCompat.Builder(this);
在代码中取消这一行。我是新手,这怎么能在这里工作是 FirebaseMessagingService.java 类中的代码
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String MessageBody){
Intent intent= new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);
notificationBuilder.setContentTitle("Hope");
notificationBuilder.setContentText(MessageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
解决方案
尝试使用:
NotificationChannel mChannel = new NotificationChannel("", "", 1);
notificationManager.createNotificationChannel(mChannel);
推荐阅读
- java - 如何使用 Files.walk 获取文件夹名称和文件名
- asp.net-core - ASP.NET Core 3.1 在类库中获取连接字符串
- c# - 在 Unity 的 UI 中显示卡片
- batch-file - 运行此 bat 文件后如何关闭 CMD
- python - 如何匹配以特定字符串开头并以其他特定字符串结尾的列?
- sql - 连接关闭后 H2 模式消失
- node.js - 当从 AWS ElastiCache Redis 中“获取”时,nodejs 中的 ioredis 什么都不做
- javascript - 使用 Javascript 自动执行带有附件的日历事件
- ruby-on-rails - ActiveStorage::DiskController#update 通过 ActiveStorage::DirectUploadsController 上传图像时抛出 422
- java - Clojure reify - 使用另一个宏自动实现Java接口?