如何解决Android版本10中的警报管理器不显示通知
我在使用系统显示通知时遇到问题。该代码适用于Android版本9,但不适用于Android版本11。 在android 10中,它仅在打开应用程序时显示通知,这与android版本9不同。
Manifest.xml(在应用程序标记中):
<receiver
android:name=".MainActivity$AlarmReceiver"
android:enabled="true"
tools:ignore="Instantiatable" />
<service
android:name=".MainActivity$NotificationService"
android:enabled="true"
tools:ignore="Instantiatable" />
这是警报的代码:
public static class Utils {
public static void setAlarm(int i,Long timestamp,Context ctx) {
AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(ctx,AlarmReceiver.class);
PendingIntent pendingIntent;
pendingIntent = PendingIntent.getbroadcast(ctx,i,alarmIntent,PendingIntent.FLAG_ONE_SHOT);
// alarmIntent.setData((Uri.parse("custom://" + System.currentTimeMillis())));
alarmManager.set(AlarmManager.RTC_WAKEUP,timestamp,pendingIntent);
}
}
public static class AlarmReceiver extends broadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
Intent service1 = new Intent(context,NotificationService.class);
service1.setData((Uri.parse("custom://" + System.currentTimeMillis())));
ContextCompat.startForegroundService(context,service1 );
}
}
public static class NotificationService extends IntentService {
private notificationmanager notificationmanager;
private PendingIntent pendingIntent;
private int NOTIFICATION_ID = 1;
Notification notification;
public NotificationService(String name) {
super(name);
}
public NotificationService() {
super("SERVICE");
}
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onHandleIntent(Intent intent2) {
String NOTIFICATION_CHANNEL_ID = getApplicationContext().getString(R.string.app_name);
Context context = this.getApplicationContext();
notificationmanager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this,MainActivity.class);
Resources res = this.getResources();
Uri soundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_ALARM);
String message = getString(R.string.new_notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final int NOTIFY_ID = 0; // ID of notification
String id = NOTIFICATION_CHANNEL_ID; // default_channel_id
String title = NOTIFICATION_CHANNEL_ID; // Default Channel
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
notificationmanager notifManager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notifManager == null) {
notifManager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
int importance = notificationmanager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id,title,importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100,200,300,400,500,400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(context,id);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(context,mIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_launcher_background) // required
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.ic_launcher_background))
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{100,400});
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID,notification);
startForeground(10587,notification);
} else {
pendingIntent = PendingIntent.getActivity(context,1,PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.ic_launcher_background))
.setSound(soundUri)
.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
.setContentText(message).build();
notificationmanager.notify(NOTIFICATION_ID,notification);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。