通知文本在 Android 的通知栏中不可见?
Posted
技术标签:
【中文标题】通知文本在 Android 的通知栏中不可见?【英文标题】:Notification Text is not visible in notification bar in Android? 【发布时间】:2016-07-26 11:16:31 【问题描述】:我在我的应用程序中使用 GCM 推送通知。但我没有收到短信和图标。它只显示白色背景。
这是我们在我的应用中接收通知的代码
public void onMessageReceived(String from, Bundle data)
//Getting the message from the bundle
String message = data.getString("message");
//Displaying a notiffication with the message
String body = null;
String title = null;
try
String notificationJSONString = data.getString("notification");
//then you can parse the notificationJSONString into a JSON object
JSONObject notificationJSON = new JSONObject(notificationJSONString );
body = notificationJSON.getString("body");
title = notificationJSON.getString("title");
catch (Exception e)
e.printStackTrace();
// sendNotification(message);
sendNotification(body,title);
//This method is generating a notification and displaying the notification
private void sendNotification(String message,String title)
Intent intent = new Intent(this, NavigationDrawerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("firsttab","notify");
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
// .setSmallIcon(R.mipmap.philips_launcher)
.setSmallIcon( getNotificationIcon())
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(sound)
.setColor(Color.parseColor("#0089C4"))
.setStyle(inboxStyle)
// .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);
/* if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
builder.setSmallIcon(R.drawable.icon_transperent);
else
builder.setSmallIcon(R.drawable.icon);
*/
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
private int getNotificationIcon()
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.not_icon : R.mipmap.philips_launcher;
【问题讨论】:
您的 GCM 发布请求是什么? 我们像这样在 messageReceive 函数中获取数据 Bundle[notification=Bundle[sound2=1, e=1, body=heyyy, icon=, badge=1, sound=default, title=Philips CORTA-NEW Notification, vibrate=1 ], collapse_key=com.philips.ordertracker] 我不相信推送消息的内容是 JSON 【参考方案1】:在消息字符串中,您将收到通知,无需进一步解析 字符串消息 = data.getString("消息"); 如果该消息没有标题,您可以在您的应用程序的基础上给出标题
【讨论】:
【参考方案2】:更改此代码并尝试一下,我希望它可以正常工作。
你终于错过了.Build();
。
发件人:
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
// .setSmallIcon(R.mipmap.philips_launcher)
.setSmallIcon( getNotificationIcon())
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(sound)
.setColor(Color.parseColor("#0089C4"))
.setStyle(inboxStyle)
// .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);
收件人:
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
// .setSmallIcon(R.mipmap.philips_launcher)
.setSmallIcon( getNotificationIcon())
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(sound)
.setColor(Color.parseColor("#0089C4"))
.setStyle(inboxStyle)
// .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent).build();
【讨论】:
感谢您的回复.. 让我检查一下并回复您 我猜 OP 从来没有检查过答案【参考方案3】:通知消息的白色背景问题已知并且正在修复,修复应该很快发布。另一种选择是使用数据消息并在 onMessageReceived 回调中自行管理通知的创建。
【讨论】:
以上是关于通知文本在 Android 的通知栏中不可见?的主要内容,如果未能解决你的问题,请参考以下文章