【问题标题】:GCM Android Notification not ExpandingGCM Android 通知未扩展
【发布时间】:2016-06-29 19:40:16
【问题描述】:

我已经为 iOS 和 android 实现了 GCM,这部分似乎工作正常。我的问题是,当通知中的文本超过一行时,我在 Android 上的通知不可扩展。我一直在寻找建议使用 BigView 调用通知的解决方案。我的通知通过 GCM 直接发送到设备,我没有在两者之间进行编辑。我不确定如何处理这个问题。

我应该只从通知中获取数据部分并在本地调用通知来解决此问题,还是在从服务器发送 GCM 时可以使用一些东西?

编辑: 我得到了扩展通知,但它仍然没有解决我的问题,因为它显示重复的通知。一个来自 GCM,另一个是本地调用的通知。我尝试将标签从 GCM 通知应用到本地通知,但它没有解决问题。

【问题讨论】:

    标签: android notifications google-cloud-messaging push


    【解决方案1】:

    尝试在通知生成器中设置此样式:

    .setStyle(new NotificationCompat.BigTextStyle().bigText("your_message_here"))
    

    【讨论】:

    • 我正在使用直接 GCM 通知。我应该重写 GcmReceiver 类并自己构建通知吗?
    • 是的 .. 这将是构建我们自己的自定义通知的更好方法 .. 我建议您使用 Firebase Cloud Messaging ..
    • 它工作但它显示与 GCM 的重复通知。我想做的是用本地通知覆盖 GCM 通知,即使我将标签从 GCM 通知应用到本地通知。
    【解决方案2】:

    为通知创建通知布局。然后在你的 textview 中设置 'lines=2' 或使用 'multiline'。

    custom_notification.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/layout"
     android:layout_width="fill_parent"
      android:layout_height="fill_parent"
        android:padding="10dp" >
        <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
        <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="Custom Notification Title" />
     <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="Custom Notification Text" />
    </RelativeLayout>
    

    将此添加到您的活动 onCreate 函数或接收器内部,以防 gcm 或 fcm(构建您的通知)

    int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);
    
        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;
    
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
    
        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound
    
        mNotificationManager.notify(1, notification);
    

    【讨论】:

    • 您使用什么来创建通知并不重要。您需要多行文本。因此,只需更改 textview 属性即可。根据您的需要。
    • 这似乎更适合我想做的事情
    • 一切都没有捷径可走。如果你想自定义 android os 的内置通知。然后你必须创建自己的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多