android - Android Oreo 通知显示在状态栏中,但不显示在点通知中
问题描述
我已经使用远程视图创建了自定义通知。他们在正常通知中工作,但没有出现在点通知中。我附上了用于创建通知的代码。
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.widget.RemoteViews;
import com.nataraj.android.justweather.MainActivity;
import com.nataraj.android.justweather.R;
public final class NotificationUtil {
static final String NOTIFICATION_3H_CHANNEL_ID = "com.nataraj.android.justweather.3_hour_notification";
private static final int NOTIFICATION_3H_ID = 1;
private static NotificationCompat.Builder getNotificationBuilder(Context context, String city, String temp, int weatherIcon) {
RemoteViews notificationLayout = new RemoteViews(context.getPackageName(), R.layout.notification_small_layout);
RemoteViews notificationBigLayout = new RemoteViews(context.getPackageName(), R.layout.notification_large_layout);
notificationLayout.setTextViewText(R.id.notification_city_name, city);
notificationBigLayout.setTextViewText(R.id.notification_city_name, city);
notificationLayout.setTextViewText(R.id.notification_temp_view, temp);
notificationBigLayout.setTextViewText(R.id.notification_temp_view, temp);
notificationLayout.setImageViewResource(R.id.notification_weather_icon, weatherIcon);
notificationBigLayout.setImageViewResource(R.id.notification_weather_icon, weatherIcon);
return new NotificationCompat.Builder(context, NOTIFICATION_3H_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationBigLayout)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(getIntent(context))
.setAutoCancel(true);
}
private static PendingIntent getIntent(Context context) {
Intent pendingIntent = new Intent(context, MainActivity.class);
pendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
return PendingIntent.getActivity(context, 0, pendingIntent, 0);
}
public static void showNotification(Context context, String city, String temp, int weatherIcon) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(NOTIFICATION_3H_ID, getNotificationBuilder(context, city, temp, weatherIcon).build());
}
}
解决方案
推荐阅读
- javascript - 无法在画布上绘制正方形
- excel - 从 Excel 引用 Access 数据库中存储的查询
- c++ - 使用 Boost 在 C++ 中从 JSON 文件中读取字符串值的向量
- mongodb - 如何在 mongoDB 中排除 $project 计算字段
- openlayers - OpenLayers:对多边形内的像素运行计算
- python-3.x - 将字节数组转换为矩阵数组
- php - 在 WordPress 中获取其他页面的页面内容时.....内容在单行中显示没有标签
- javascript - 第二个文本框只允许较小的数字
- c++ - 在 C++ 中实现具有相同方法名称和不同参数的多个接口
- javascript - JS 在特定 div 中定位图像,即使它们共享类