首页 > 解决方案 > 通知不显示 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());

}

}

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


尝试使用:

NotificationChannel mChannel = new NotificationChannel("", "", 1);
notificationManager.createNotificationChannel(mChannel);

推荐阅读