android - 无法在 android Pie 中创建通知
问题描述
我正在尝试为我的 android 应用程序实现通知,它在除 android 9 之外的几乎所有版本上都能正常工作(我没有在 android 8 上检查它)。我在androidx..
public static void openActivityNotification(Context context){
NotificationCompat.Builder nc = new NotificationCompat.Builder(context);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.mipmap.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Notification Demo");
nc.setContentText("Click please");
nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY, nc.build());
}
如何使其适用于android 9?
解决方案
您似乎缺少 Android O 及更高版本所需的通知渠道。文档:https ://developer.android.com/training/notify-user/channels 试试这个示例:
private String CHANNEL_ID;
private void createNotificationChannel() {
CharSequence channelName = CHANNEL_ID;
String channelDesc = "channelDesc";
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
channel.setDescription(channelDesc);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
assert notificationManager != null;
NotificationChannel currChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
if (currChannel == null)
notificationManager.createNotificationChannel(channel);
}
}
public void createNotification(String message) {
CHANNEL_ID = UiUtil.getStringSafe(R.string.app_name);
if (message != null ) {
createNotificationChannel();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(UiUtil.getStringSafe(R.string.app_name))
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Uri uri =RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(uri);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
int notificationId = (int) (System.currentTimeMillis()/4);
notificationManager.notify(notificationId, mBuilder.build());
}
}
推荐阅读
- python - Python:我的全局变量未定义
- algorithm - 使用中序遍历的二叉树序列化和反序列化
- ruby-on-rails - 由于sign_in不起作用,如何使用设计更改rails中的current_user
- inno-setup - Inno Setup 获取默认浏览器
- c++ - C++,printf vs cout 性能
- javascript - 使用路由传递道具时,反应 Js 的组件中无法接收 URL 参数
- prolog - Prolog“,”运算符未按预期工作?
- snowflake-cloud-data-platform - 尝试通过 ODBC 查询 Snowflake 时,Unixodbc 产生“找不到文件”错误
- python - 检查转义序列形式的 HTML 标签是否格式正确
- arrays - 二维可分配整数数组作为 Fortran 中的一种类型