我想让我的应用在前台取消推送通知。它可以在我的模拟器上正常工作(在前景和背景上接收),但是当推送到前景上的手机时,应用程序崩溃。 这是我的代码:
if (android.os.Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,Notificationmanager.IMPORTANCE_HIGH);
channel.setDescription(CHANNEL_DESC);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody());
Notificationmanager manager = (Notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
manager.notify(1,builder.build());
}
我的手机具有android 7.1版本,因此应用程序与NotificationChannels一致。据我了解,NotificationChannels不适用于该旧版本的android。我想找到一种方法,如何为小于25的sdk版本重写NotificationCompat.Builder。如果我尝试以旧样式编写它,Android Studio会说明该方法已被废弃。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
使NotificationCompat.Builder在旧的sdk版本上工作的正确方法是什么?或者我该如何在不支持NotificationChannels的android版本中使我的应用程序在前台接收消息?