6

我无法让Android的大文本通知按此处所述的方式工作:NotificationCompat.BigTextStyle。这里是我用来显示通知的代码。我知道所有数据都可以正确回来,因为我可以将它显示在控制台上并作为传统的内容文本和标题。难道我做错了什么?我是否还需要在其他地方定义bigTestStyle?希望你们之前做过这件事,并知道可能会丢失什么。谢谢。通知大文本Android GCM

As you can see the big text message text is not displaying

我的代码:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
    bigTextStyle.bigText(extras.getString("message")); 
    NotificationCompat.Builder bigTextNotification = new NotificationCompat.Builder(context) 
      .setContentTitle("My App") 
      .setContentIntent(pendingIntent) 
      .setContentText("Message:") 
      .setSound(soundUri) 
      .setTicker(extras.getString("message")) 
      .setWhen(System.currentTimeMillis()) 
      .setSmallIcon(R.drawable.splash) 
      .setAutoCancel(true) 
      .setVibrate(new long[] { 0, 100, 200, 300 }) 
      .setStyle(bigTextStyle); 
    final int notificationId = (int) (System.currentTimeMillis()/1000L); 
    NotificationManager thisone = (NotificationManager) getApplicationContext() 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    thisone.notify(notificationId, bigTextNotification.build()); 
+0

我有一个micromax画布A74,Android版本4.2.2。使用'NotificationCompat'对我很好。但是,当我使用'Notification.Bulider'时,'BigTextStyle'工作正常,但正常的通知样式不起作用。它只是显示没有任何文字的通知图标。 – 2014-08-04 21:12:00

回答

4

NotificationCompat.BigTextStyle文件:

辅助类生成大幅面的通知,其中包括大量的文字。 如果平台不提供大幅面通知,则此方法不起作用。用户将始终看到正常的通知视图。

也许你的平台不支持大格式的通知。

编辑:

在另一方面,它可能是您的问题是在这里:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
bigTextStyle.bigText(extras.getString("message")); 

您未使用的bigText返回值。

尝试将其更改为:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
bigTextStyle = bigTextStyle.bigText(extras.getString("message")); 

或邮寄至:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().bigText(extras.getString("message")); 

EDIT2:

对于较旧的Android版本德,崔根源通知布局:

protected void onMessage(Context context, Intent intent) { 
    // Extract the payload from the message 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     String message = (String) extras.get("message"); 
     String title = (String) extras.get("title");     

     // add a notification to status bar 
     NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent myIntent = new Intent(this,MyActivity.class); 
     Notification notification = new Notification(R.drawable.notification_image, title, System.currentTimeMillis()); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
     contentView.setImageViewResource(R.id.image, R.drawable.notification_image); 
     contentView.setTextViewText(R.id.title, title); 
     contentView.setTextViewText(R.id.text, message); 
     notification.contentView = contentView; 
     notification.contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
     mManager.notify(0, notification); 
     PowerManager pm = (PowerManager) 
     context.getSystemService(Context.POWER_SERVICE); 
     WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(15000); 
    } 
} 
+0

这些都没有工作。不知道是否bigTextStyle是作为bigTextNotification的一部分构建的 – centree 2013-05-10 20:58:11

+0

在这种情况下,您的设备或Android版本可能不支持大幅面通知。 – Eran 2013-05-10 21:06:50

+0

我正在使用全新的S3。 – centree 2013-05-10 21:07:46