Android GCM通知大文本

6
我在使用Android的Big Text通知时遇到了问题,无法按照这里所述的方式正常工作:NotificationCompat.BigTextStyle。以下是我用来显示通知的代码。我知道所有数据都正确返回了,因为我可以在控制台上和传统的内容文本和标题上显示它。我是否做错了什么?我需要在其他地方定义bigTestStyle吗?希望你们中的一位以前做过这个并知道可能缺少什么。谢谢。
如图所示,大文本消息文本未显示:
我的代码:
    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());

我有一部Micromax Canvas A74手机,安卓版本为4.2.2。使用NotificationCompat对我来说很好用。然而,当我使用Notification.Builder时,BigTextStyle可以正常工作,但普通的通知样式却无法正常工作。它只显示通知图标,没有任何文本。 - Sash_KP
1个回答

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"));

编辑2:

为旧版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);
    }
}

这两个都没有起作用。不确定bigTextStyle是否作为bigTextNotification的一部分被构建。 - centree
在这种情况下,可能是您的设备或Android版本不支持大尺寸通知。 - Eran
既然您有了新设备,我假设您已经安装了新的Android版本。我建议您尝试使用Notification.BigTextStyle而不是NotificationCompat.BigTextStyle,看看是否有效。 - Eran
你是否拥有兼容性库的最新版本?也许更新的版本会更好用。 - Eran
当使用NotificationCompat类时,它是否适用于你的新设备?还是只有在使用API16类时才适用?如果在新设备上使用NotificationCompat正常工作,则不支持它的设备根本不支持。如果NotificationCompat在新设备上无法工作,则可能是该类中存在错误,或者您使用该类的方式存在问题。 - Eran
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接