未显示前台的android通知(Oreo) [英] Notification android on foreground not displayed (Oreo)

查看:91
本文介绍了未显示前台的android通知(Oreo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序位于前景中时,我正在尝试使用Firebase显示通知.当我从服务器推送通知时调用onMessageReceived方法,但通知未显示.

这是我的代码:

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());
                        }
                    }
                }
            });
    }
}

在我的logcat中,我可以看到:

FCM消息通知正文:202018105166

FCM-来自:1049809400953

FCM通知

我关注了 https://developer.android.com/training/notify-user/build-notification.html#builder

我在奥利奥(Oreo)上奔跑

解决方案

在此处找到答案: Android前台服务通知未显示

这是一个奥利奥问题,感谢@Yashaswi N P

解决方案

在此处找到答案:

感谢@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 :

FCM-Message Notification Body: 202018105166

FCM-From: 1049809400953

FCM-Notif

I followed https://developer.android.com/training/notify-user/build-notification.html#builder

I'am running on Oreo

SOLUTION

Found the answer here : Android foreground service notification not showing

It was an Oreo issue, thank to @Yashaswi N P

解决方案

Found the answer here : Android foreground service notification not showing

Need 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆