无法滑动并删除前台服务的通知
Cant swipe and remove notification on foreground service
我有 3 个活动:activity3.java、notificationService.java 和 firebaseNotification.java。
在 activity3.java 我做了一个通知频道。
代码:
public void openChannel(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(
"deliveryinfo",
"Delivery Information",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager =getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
notificationService.java是一个服务,代码是:
public class notificationService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent intent1 = new Intent(getApplicationContext(), activity3.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "deliveryinfo")
.setContentTitle("Condition Changed")
.setContentText(intent.getStringExtra("extra"))
.setSmallIcon(R.drawable.deliver)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOngoing(true)
.setAutoCancel(true);
.build();
startForeground(1,notification);
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在 firebaseNotification.java 中,我从 firebase 获取数据并将其传递给 notificationService。
Intent intent = new Intent(getApplicationContext(), notificationService.class);
intent.putExtra("extra", snapshot.child("condition").getValue().toString());
startService(intent);
但是我有一个问题。我无法清除该通知。我到处找解决方法,但是没有相关信息。
P.S。 firebaseNotification.java 扩展应用程序及其在清单中声明的 (android:name=".firebaseNotification")。 我尝试在 startForeground(1,notification)
和 notificationmanger.cancel()
下的 notificationService 中写 stopSelf()
但它没有帮助。
Foreground services must display a Notification.
https://developer.android.com/guide/components/services
摆脱通知的唯一方法是让您的服务不在前台运行。
我有 3 个活动:activity3.java、notificationService.java 和 firebaseNotification.java。 在 activity3.java 我做了一个通知频道。 代码:
public void openChannel(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(
"deliveryinfo",
"Delivery Information",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager =getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
notificationService.java是一个服务,代码是:
public class notificationService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent intent1 = new Intent(getApplicationContext(), activity3.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "deliveryinfo")
.setContentTitle("Condition Changed")
.setContentText(intent.getStringExtra("extra"))
.setSmallIcon(R.drawable.deliver)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOngoing(true)
.setAutoCancel(true);
.build();
startForeground(1,notification);
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在 firebaseNotification.java 中,我从 firebase 获取数据并将其传递给 notificationService。
Intent intent = new Intent(getApplicationContext(), notificationService.class);
intent.putExtra("extra", snapshot.child("condition").getValue().toString());
startService(intent);
但是我有一个问题。我无法清除该通知。我到处找解决方法,但是没有相关信息。
P.S。 firebaseNotification.java 扩展应用程序及其在清单中声明的 (android:name=".firebaseNotification")。 我尝试在 startForeground(1,notification)
和 notificationmanger.cancel()
下的 notificationService 中写 stopSelf()
但它没有帮助。
Foreground services must display a Notification.
https://developer.android.com/guide/components/services
摆脱通知的唯一方法是让您的服务不在前台运行。