java - 通知构建显示标题和图像,但不显示内容

标签 java android firebase push-notification

我正在向已安装我的应用程序(包含图像、标题和内容)作为数据负载的客户推送通知。效果很好,但内容不空白。我什至用标题和虚拟文本替换了内容,而不是来自服务器,但内容字段仍然是空白的。所以我确信我也得到了内容字段,但我的代码本身一定存在一些问题。

这是我的代码

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(Config.title)
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentText(Config.content)
            .setContentIntent(pendingIntent)
            .setStyle(style)
            .setLargeIcon(bitmap)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX);


    notificationManager.notify(1, notificationBuilder.build());
}

我推送数据的方法是

{
    "data": {
        "title": "Check our New Recipe",
        "content" : "Check Out This Awesome Game!",
        "imageUrl": "http://h5.4j.com/thumb/Ninja-Run.jpg"
    },
    "to": "/topics/food"
}

我也替换

 .setContentText(Config.content)

 .setContentText("This is my content")

但它仍然是不可见的。

我遵循的教程是

https://android.jlelse.eu/android-push-notification-using-firebase-and-advanced-rest-client-3858daff2f50

enter image description here

最佳答案

您可以尝试使用此代码来显示您的内容和图像

private void sendNotification(String messageBody, Context context) {
        Log.e("rsp", messageBody);

        String title = "Notification";
        String desciption = messageBody;

        Intent intent = null;

        try {
            JSONObject message = new JSONObject(messageBody);
            title = message.getString("title");
            desciption = message.getString("msg");
            intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NotificationCompat.Builder builder = null;
        final int NOTIFY_ID = 0; // ID of notification
        String id = "mychannel"; // default_channel_id

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

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (notifManager == null) {
            notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notifManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, title, importance);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notifManager.createNotificationChannel(mChannel);
            }

            builder = new NotificationCompat.Builder(context, id)
                    .setContentTitle(title)
                    .setContentText(desciption)
                    .setContentIntent(pendingIntent)
                    .setSound(defaultSoundUri)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else {
            builder = new NotificationCompat.Builder(context, id);
            intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            builder.setContentTitle(title)
                    .setContentText(desciption)                            // required
                    .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setTicker("Accept your request")
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setPriority(Notification.PRIORITY_HIGH);
        }

        Notification notification = builder.build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(NOTIFY_ID, notification);
    }

您可以在此处阅读完整代码:Firebase push notification

关于java - 通知构建显示标题和图像,但不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813052/

相关文章:

java - 我们可以遍历 Amazon S3 中的完整对象集吗

java - Spring项目模板不显示模板

android - 错误 : Multi dex requires Build Tools version

swift - 如何转换 [String : Any] dictionary into a double?

javascript - 表单提交未显示在 Firebase 中

java - 将 Epoch 转换为日期和时间 - HADOOP

java - 不要关闭外部点击时的弹出窗口

java - Android Socket ReadLine 但具有不同的终止符

android - 嵌套的 LinearLayouts 不工作

angularjs - firebase 过滤器和分页