我将通知发送给安装了我的应用程序的客户,其中包含数据,图片,标题和内容的数据。它运作良好,但内容并非空白。我什至用标题替换了内容,还用服务器上的伪文本替换了内容,但是内容字段仍然为空。所以我确定我也正在获取内容字段,但是我的代码本身肯定存在一些问题。
这是我的代码
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")
但它仍然是不可见的。
我遵循的教程是
答案 0 :(得分:0)
您可以尝试使用此代码显示您的内容和图片
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