Android Studio 无法在前台服务上滑动和删除通知

dojqjjoe  于 2022-12-30  发布在  Android
关注(0)|答案(1)|浏览(208)

我有3项活动:activity3.javanotificationService.java和firebaseNotification.java。在www.example.com中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;
    }
}

在www.example.com中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”)。我尝试在notificationService中的startForeground(1,notification)notificationmanger.cancel()下写入stopSelf(),但没有帮助。

btqmn9zl

btqmn9zl1#

前台服务必须显示通知。
https://developer.android.com/guide/components/services
消除通知的唯一方法是使服务不在前台运行。

相关问题