gpt4 book ai didi

java - Android 7.1 及更低版本显示双通知

转载 作者:行者123 更新时间:2023-12-02 08:58:14 26 4
gpt4 key购买 nike

我在我的应用程序中添加了预定通知,一切正常,除了低于 7.1 的 Android 版本会显示双通知。

这是我的通知发布者类(BroadcastReciever)

public class NotificationPublisher extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
this.context = context;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}

sendNotification("title", "body", notificationId, activity);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.enableVibration(true);
channel.setDescription(CHANNEL_DESCRIPTION);
channel.setLightColor(Color.RED);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}

public void sendNotification(String title, String body, int id, Class activity) {
Intent intents = new Intent(context, activity);
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intents, PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();

NotificationManagerCompat.from(context).notify(new Random().nextInt(), notification);
}

}

这是我安排通知的代码:

public static void createNotification(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("app.action.DISPLAY_NOTIFICATION");
intent.putExtra("notificationId", notificationID);

Calendar calReminder = Calendar.getInstance();
calReminder.setTimeInMillis(System.currentTimeMillis());
calReminder.set(Calendar.HOUR_OF_DAY, 16);
calReminder.set(Calendar.MINUTE, 30);

PendingIntent broadcast = PendingIntent.getBroadcast(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calReminder.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}

这是我在应用程序启动时的 MainActivity 代码:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private NotificationPublisher broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

broadcastReceiver = new NotificationPublisher();

setNotificationScheduler(this);
}

public static void setNotificationScheduler(Context context) {
AppNotifications.createNotification(context);
}

@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter("app.action.DISPLAY_NOTIFICATION");
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(broadcastReceiver, intentFilter);
}

}

我不明白为什么它在 android 7.1 及更低版本上显示双重通知。

另外只是为了确保:在启动时创建通知是否错误?还有更多按书本规定的方法吗?

提前致谢。

最佳答案

 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

将此代码添加到您的通知发布者类中。

关于java - Android 7.1 及更低版本显示双通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com