android推送通知显示空白内容
Posted
技术标签:
【中文标题】android推送通知显示空白内容【英文标题】:android push notification shows blank content 【发布时间】:2014-10-29 07:01:33 【问题描述】:我在 android 中使用谷歌云消息显示警报。它在某些设备和模拟器中工作,但不适用于所有设备,如三星 Galaxy Grand 和我的一些模拟器,它只显示通知的图标和标题,而消息是空白的。
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE
.setStyle(new NotificationCompat.BigTextStyle().bigText(mes))
.setContentText(mes);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
【问题讨论】:
你遇到任何错误 检查您身边的消息密钥。和服务器端一样吗?? 不,我没有收到任何错误。唯一的问题是,在某些设备中,通知标题、图标、消息显示完美。但同样的通知将只显示图标、标题,但消息将为空白。 【参考方案1】:试试这个代码。
private void generateNotification(Context context, String message)
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = "New POC Alert";
Intent notificationIntent = new Intent(context, null);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
//Mainfest权限
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="vibrate" />
【讨论】:
【参考方案2】:请试试这个。
// Put the message into a notification and post it.
// MainActivity is the activity which you wish to load. Setting setWhen() with 0 will not publish time.
private void sendNotification(String msg)
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.your_app_icon)
.setContentTitle(getResources().getText(R.string.your_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setWhen(0).setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentText(msg)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
我希望您拥有所需的权限。 参考:GCM Reference
【讨论】:
嘿,感谢您的回复,但我注意到,如果我在 ContentText 中提供静态文本,那么它应该会出现。只有当我使用变量时才会出现问题,然后它不会出现 您是否尝试过调试?我希望你能正确解析它。我认为提供静态消息行不通,作为参数传递也行不通。【参考方案3】:这样试试可能对你有帮助
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class), 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Siom Alert")//TITLE CAN BE TSKEN FROM HERE
.setStyle(new NotificationCompat.BigTextStyle().bigText(mes))
.setContentText(mes);
else
//code for simple notification
【讨论】:
嘿,感谢您的回复,但我注意到,如果我在 ContentText 中提供静态文本,那么它应该会出现。只有当我使用变量时才会出现问题,然后它不会出现以上是关于android推送通知显示空白内容的主要内容,如果未能解决你的问题,请参考以下文章
避免在 Android 中使用通知推送从 GCM 显示自动通知