这是一个奥利奥问题,感谢@Yashaswi N P 解决方案在此处找到答案:未显示Android前台服务通知需要使用Oreo处理频道 mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager); mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE); mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());@TargetApi(26)private void createChannel(NotificationManager notificationManager) { String name = "FileDownload"; String description = "Notifications for download status"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel mChannel = new NotificationChannel(name, name, importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.BLUE); notificationManager.createNotificationChannel(mChannel);}感谢@Yashaswi N P I'm trying to display an notification with firebase when the app is in the foreground. The onMessageReceived method is called when I push the notification from the server, but the notification is not displayed.Here my code :public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(final RemoteMessage remoteMessage) { Timber.d("FCM-From: " + remoteMessage.getFrom()); new Handler(Looper.getMainLooper()).post(new Runnable() { public void run() { if (remoteMessage.getNotification() != null) { Timber.d("FCM-Message Notification Body: " + remoteMessage.getNotification().getBody()); NotificationCompat.Builder builder = new NotificationCompat.Builder( getApplicationContext(), "CHANNEL_NOTIF") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("test") .setContentText("test content"); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (manager != null) { Timber.d("FCM-Notif"); manager.notify(1, builder.build()); } } } }); }}In my logcat I can see :I followed https://developer.android.com/training/notify-user/build-notification.html#builderI'am running on OreoSOLUTIONFound the answer here : Android foreground service notification not showingIt was an Oreo issue, thank to @Yashaswi N P 解决方案 Found the answer here : Android foreground service notification not showingNeed to handle channel with Oreo mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager); mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE); mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());@TargetApi(26)private void createChannel(NotificationManager notificationManager) { String name = "FileDownload"; String description = "Notifications for download status"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel mChannel = new NotificationChannel(name, name, importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.BLUE); notificationManager.createNotificationChannel(mChannel);}Thank to @Yashaswi N P 这篇关于未显示前台的android通知(Oreo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 23:09