java - 为什么我的通知在 Android > 7 上不起作用?
问题描述
我需要有关通知的帮助。这不适用于 Android > 7。我每天上午 10 点使用广播接收器重复我的通知我希望我的通知在 Android 最小 19 到最大 29 上工作。可能吗?请问有什么帮助吗?
接收者 :
public class AlarmReceiverNewUser extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String userName = App.SP().getString(App.SHARED_PREFERENCES_USER_NAME, "");
final String textes[] = {context.getResources().getString(R.string.textNotification1), context.getResources().getString(R.string.textNotification2),
context.getResources().getString(R.string.textNotification3), context.getResources().getString(R.string.textNotification4),
context.getResources().getString(R.string.textNotification5), context.getResources().getString(R.string.textNotification6),
context.getResources().getString(R.string.textNotification7)};
int idx = new Random().nextInt(textes.length);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context, FreeCalmBreathActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntentSecond = PendingIntent.getActivity(context, 42, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context). //Enlever issou si ça bug
setSmallIcon(R.drawable.ic_logo).
setContentIntent(pendingIntentSecond).
setContentText(userName + (textes[idx])).setStyle(new NotificationCompat.BigTextStyle()
.bigText(userName + (textes[idx]))).
addAction(R.drawable.ic_check_black_24dp, App.getContext().getResources().getString(R.string.Access), pendingIntentSecond).
setContentTitle(App.getContext().getResources().getString(R.string.app_name)).
setSound(alarmSound).
setNumber(7).
setChannelId(String.valueOf(42)).
setAutoCancel(true);
notificationManager.notify(42, builder.build());
setNotification(context, 42);
}
private void setNotification(Context context, int id) {
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, 10);
calender.set(Calendar.MINUTE, 0);
calender.set(Calendar.SECOND, 0);
Intent intent = new Intent(context, AlarmReceiverNewUser.class);
intent.putExtra("NotificationId", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis() +
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
解决方案
您需要使用 Notification Builder 创建一个通道以在 Android O 中发送通知
NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
或者您可以通过检查 android 版本来发送通知
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
}
或者您也可以设置频道 ID
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, Name, Importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("")
.setContentText("")
.setChannelId(CHANNEL_ID)
.build();
更多详情,您可以查看Android官方文档
推荐阅读
- javascript - 如何根据数据字段动态显示我的元素?
- android - 为什么我的 Android Studio 语言变得很奇怪?
- c# - URL 重定向到变量
- c - 我试图在 VS 中运行一个简单的 c 代码,但这表明,该怎么办?
- angular - Angular 新的日期范围过滤器错误?
- windows - 无法使用 docker-compose 将 docker 文件夹挂载到主机中
- python - Python中的静态方法可以访问/修改类变量吗?
- mongodb - 如何在 MongoDB 中投影 addFields 属性
- javascript - 使用 jQuery 将常量值与实际值进行比较
- python - 如何在 DynamoDB 中查询条件属性