Android 中的长通知标题在应用程序被终止时被裁剪
Long Notification Title in Android gets cropped when the app is killed
我们正在推送来自内置 React JS 的 Web 应用程序的通知。通知标题有大约 95 个字符,在 phone 上收到时会被裁剪。我们如何像在 Inshorts 应用程序中那样包装标题?当应用程序位于前台时,我们没有任何问题。
这是我们用来推送通知的 React JS 代码:
message = {
notification: {
title:"Long title with 95 characters",
seoLink:"google.co.in",
image: "abc.jpg",
content_available: 'true'
},
data: {
title:"Long title with 95 characters",
body: "body msg",
seoLink:"google.co.in",
image: "abc.jpg",
},
to: fcmToken
};
这是我们在 Android 中使用的代码 Native:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(image)
.setContentText(title)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setVibrate(vibrate)
.setLights(0xff00ff00, 300, 100)
.setWhen(System.currentTimeMillis());
如果您需要自定义布局,可以将 NotificationCompat.DecoratedCustomViewStyle
应用于您的通知。
创建名为 notification_small.xml
的自定义 xml 文件:
<TextView
android:layout_width="wrap_content" //here for the wrap content for the title
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/notification_title"
android:id="@+id/notification_title"
style="@style/TextAppearance.Compat.Notification.Title" />
然后像这样构建你的通知:
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
您可以在此处阅读有关自定义通知布局的更多信息:
https://developer.android.com/training/notify-user/custom-notification
对于您在后台遇到的问题:
这是我们用来推送通知的 React JS 代码:
message = {
notification: {
title:"Long title with 95 characters",
seoLink:"google.co.in",
image: "abc.jpg",
content_available: 'true'
},
data: {
title:"Long title with 95 characters",
body: "body msg",
seoLink:"google.co.in",
image: "abc.jpg",
},
to: fcmToken
};
这是我们在 Android 中使用的代码 Native:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(image)
.setContentText(title)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setVibrate(vibrate)
.setLights(0xff00ff00, 300, 100)
.setWhen(System.currentTimeMillis());
如果您需要自定义布局,可以将 NotificationCompat.DecoratedCustomViewStyle
应用于您的通知。
创建名为 notification_small.xml
的自定义 xml 文件:
<TextView
android:layout_width="wrap_content" //here for the wrap content for the title
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/notification_title"
android:id="@+id/notification_title"
style="@style/TextAppearance.Compat.Notification.Title" />
然后像这样构建你的通知:
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
您可以在此处阅读有关自定义通知布局的更多信息:
https://developer.android.com/training/notify-user/custom-notification
对于您在后台遇到的问题: