Android Kotlin:FirebaseMessingService已收到消息,但未显示通知

Android Kotlin:FirebaseMessingService已收到消息,但未显示通知,android,firebase,push-notification,notifications,kotlin,Android,Firebase,Push Notification,Notifications,Kotlin,我创建了一个扩展FireBaseMissingService的服务,该接收预期将接收数据通知 接收到的onMessageReceived能够很好地接收数据负载并正确提取数据。然后调用sendNotification()。但通知中没有显示任何内容 这是我的发送通知功能 private fun sendNotification(item: Item) { val intent = Intent(this, ItemNotificationActivity::class.java)

我创建了一个扩展FireBaseMissingService的服务,该接收预期将接收数据通知

接收到的onMessageReceived能够很好地接收数据负载并正确提取数据。然后调用sendNotification()。但通知中没有显示任何内容

这是我的发送通知功能

private fun sendNotification(item: Item) {

    val intent = Intent(this, ItemNotificationActivity::class.java)

    intent.putExtra(ItemDetailFragment.ARG_ITEM, item)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
    intent.setAction( System.currentTimeMillis().toString())

    val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT)

    //        String channelId = getString(R.string.default_notification_channel_id);
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, "channelId")
            .setSmallIcon(R.drawable.ic_new_releases_black_24dp
            )
            .setContentTitle(getString(R.string.app_name))
            .setContentText(item.title.trim())
                                    .setAutoCancel(true)
                                    .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel("channelId" + item.id,
                item.title,
                NotificationManager.IMPORTANCE_DEFAULT)
        notificationManager.createNotificationChannel(channel)
    }
    val notification = notificationBuilder.build()
    notification.flags = notification.flags or Notification.FLAG_AUTO_CANCEL

    notificationManager.notify(item.id /* ID of notification */, notification)
}

我的通知有什么问题

想解释一下吗?我已经为函数添加了注释,如果你没有得到什么的话。让我知道这个疑问。我会向你解释的。现在检查一下。每一行都有解释。还有任何疑问,请说明我应该用你的代码替换我的全部代码吗?问题中的
sendNotification()
方法有什么问题?如果您比较缺少的几个属性以进行设置,最好用您的代码替换我的代码并重试。问题在于重要性,为什么不显示重要性是否为NotificationManager.importance\u默认值?
 public class MyFireBaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e("Notification", "From : " + remoteMessage.getFrom());
        Log.e("Notification", "Data : " + remoteMessage.getData().toString());
        String type = remoteMessage.getData().get("notificationType");
        try {
            sendNotification(remoteMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     */
    private void sendNotification(RemoteMessage remoteMessage) throws UnsupportedEncodingException {
        String title, message;
        title = getString(R.string.app_name);
        message = "" + remoteMessage.getData().get("text"); //instead of text you can have your key getting received in notification

       // Pending intent will perform redirection on clicking Notification

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("fromNotification", true);
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                (int) System.currentTimeMillis(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
                        | PendingIntent.FLAG_CANCEL_CURRENT);


        // Let's create notification to be displayed


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
        notiStyle.setSummaryText(message);
        Bitmap remotePicture = null;     // This will be used only when you will have image url in notification and want to display image as a notification.

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // incase you want to set your custom sound for notification

        notificationBuilder
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.app_purple))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(contentIntent);

     // If there is an image URL to be displayed, get the bitmap from URL and pass it to notification builder

          if (remoteMessage.getData().containsKey("image")) {  
                try {
                    remotePicture = BitmapFactory.decodeStream((InputStream) new URL(remoteMessage.getData().get("image")).getContent());
                    notiStyle.bigPicture(remotePicture);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (remotePicture != null) notificationBuilder.setStyle(notiStyle);
            }

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

      // If you are using device with OS OREO or ABOVE

       if (VERSION.SDK_INT >= VERSION_CODES.O) {
            String channelId = "your_app_notificaiton_channel_id";
            CharSequence channelName = "Your App Channel";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);

            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .build();

            notificationChannel.setSound(soundUri, audioAttributes);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            notificationManager .createNotificationChannel(notificationChannel);
            notificationBuilder.setChannelId(channelId);
        }

        notificationManager.notify((int) System.currentTimeMillis() /* ID of notification */, notificationBuilder.build());
    }
}