首页 > 解决方案 > onMessageRecieved() 被调用,执行其代码但不显示通知

问题描述

我正在关注本教程,我正在使用 FCM 发送推送通知,但由于某种原因,代码似乎不起作用。在 onMessageRecieved() 中,我正在构建通知,使手机振动并显示通知,由于某种原因,手机振动,但没有显示通知。这是代码:

Intent intent=new Intent(this, WelcomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent=PendingIntent.getActivity(this
                    ,0
                    ,intent
                    ,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(getString(R.string.app_name));
            notificationBuilder.setContentText(message);
            Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(soundUri);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
            notificationBuilder.setAutoCancel(true);
            Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(1000);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager=(NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0,notificationBuilder.build());

如果有人能告诉我出了什么问题,那就太棒了..谢谢:)

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


从您的代码和通知没有出现的事实来看,我假设您正在使用 Android 版本高于 8.0 的设备进行测试。

对于 Android 8.0 (Oreo) 及更高版本,您需要使用通知渠道,以显示通知。有关更多详细信息,请参阅此链接


推荐阅读