android - 通知大文本 Android GCM

标签 android android-layout push-notification google-cloud-messaging

我无法按照此处记录的方式让 Android 的大文本通知正常工作:NotificationCompat.BigTextStyle .这是我用来显示通知的代码。我知道所有数据都正确返回,因为我可以将其显示在控制台上并显示为传统的内容文本和标题。难道我做错了什么?我还需要在其他地方定义 bigTestStyle 吗?希望你们中的一个人以前做过这个并且知道可能会遗漏什么。谢谢。

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

最佳答案

来自 NotificationCompat.BigTextStyle文档:

Helper class for generating large-format notifications that include a lot of text. If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.

可能您的平台不支持大格式通知。

编辑:

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

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 版本的 Costom 通知布局:

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

关于android - 通知大文本 Android GCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16490048/

相关文章:

android - 如何访问 onDataChange 完成后返回的值?

android - ProgressDialog 不会立即出现

java.lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.support.v7.widget.RecyclerView$LayoutParams

java - 在 Android Studio 中关闭应用程序时推送通知无法正常工作

javascript - Web 推送通知在 Firefox 浏览器中不显示消息

ios - 取消或重新安排本地通知的推送通知?

android - 管理堆大小

java - 如何删除 Firebase 事件监听器?

Android TextView 右对齐

java - 是否有可能在循环中设置多个 ImageView 的比例?