通知大文本 Android GCM [英] Notification Big Text Android GCM

查看:18
本文介绍了通知大文本 Android GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 Android 的大文本通知按照此处记录的方式工作: 文档:

<块引用>

Helper 类,用于生成包含大量文本的大型通知.如果平台不提供大幅面通知,此方法无效.用户将始终看到正常的通知视图.

也许您的平台不支持大格式通知.

另一方面,您的问题可能出在此处:

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

旧版 Android 的 Costom 通知布局:

protected void onMessage(Context context, Intent intent) {//从消息中提取有效载荷Bundle extras = intent.getExtras();如果(额外的!= null){String message = (String) extras.get("message");String title = (String) extras.get("title");//向状态栏添加通知NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Intent myIntent = new Intent(this,MyActivity.class);通知通知 = 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);通知.contentView = contentView;notification.contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);mManager.notify(0, 通知);电源管理器 pm = (电源管理器)context.getSystemService(Context.POWER_SERVICE);WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");wl.acquire(15000);}}

I'm having trouble getting Android's Big Text notifications to work as documented here: NotificationCompat.BigTextStyle. Here's the code I'm using to display notifications. I know all the data is coming back correctly because I can get it to display on the console and as the traditional content text and title. Am I doing something wrong? Do I need to define bigTestStyle somewhere else as well? Hopefully one of you has done this before and knows what could be missing. Thanks.

My code:

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

解决方案

From NotificationCompat.BigTextStyle documentation :

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.

Perhaps your platform doesn't support large-format notifications.

EDIT :

On the other hand, it's possible your problem is here :

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

You are not using the return value of bigText.

Try to change it to :

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

or to :

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

EDIT2 :

Costom Notification Layout for older Android versions :

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 GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆