Notifications Android:如何修复JobIntentService中的BroadcastReceiver?

Notifications Android:如何修复JobIntentService中的BroadcastReceiver?,notifications,jobintentservice,Notifications,Jobintentservice,我有一个AlarmManager的活动,它触发广播接收器。BroadcastReceiver启动JobIntentService以向用户发送通知 当用户点击“全部清除”或滑动以取消通知时,我希望setDeleteIntent()将我在SharedReferences文件中设置的计数器变量“totalMessages”重置为零。它不是重置为零。我错过了什么 public class AlarmService extends JobIntentService { static final

我有一个AlarmManager的活动,它触发广播接收器。BroadcastReceiver启动JobIntentService以向用户发送通知

当用户点击“全部清除”或滑动以取消通知时,我希望setDeleteIntent()将我在SharedReferences文件中设置的计数器变量“totalMessages”重置为零。它不是重置为零。我错过了什么

public class AlarmService extends JobIntentService {

    static final int JOB_ID = 9999;
    public static final String NOTIFICATIONS_COUNTER = "NotificationsCounter";
    private static final int REQUEST_CODE_DELETE_INTENT = 1;
    int totalMessages = 0;

    static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, AlarmService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {

        IntentFilter filter = new IntentFilter();
        filter.addAction("notification_cleared");
        registerReceiver(receiver, filter);

        sendNotification();
    }

    private void sendNotification() {

        int notifyID = 1;

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        SharedPreferences sp = getSharedPreferences(NOTIFICATIONS_COUNTER, Context.MODE_PRIVATE);
        totalMessages = sp.getInt("total-messages", 0); //initialize to 0 if it doesn't exist
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("total-messages", ++totalMessages);
        editor.apply();

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_announcement_white_24dp)
            .setContentText("")
            Intent i = new Intent(this, AlarmService.class);
            i.setAction("notification_cleared");
            PendingIntent deleteIntent = PendingIntent.getBroadcast(this,REQUEST_CODE_DELETE_INTENT,i,PendingIntent.FLAG_CANCEL_CURRENT);
            mBuilder.setDeleteIntent(deleteIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mBuilder.setSubText(String.valueOf(totalMessages));
        }
        else {
            mBuilder.setNumber(totalMessages);
        }

        if (notificationManager != null) {
            notificationManager.notify(notifyID, mBuilder.build());
        }
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if (action.equals("notification_cleared")) {

                // Reset the Notifications counter ("total-messages") to zero since the user
                // clicked on "CLEAR ALL" Notification or swiped to delete a Notification.
                SharedPreferences sp1 = getSharedPreferences(NOTIFICATIONS_COUNTER, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp1.edit();
                editor.clear();
                editor.apply();
                totalMessages = sp1.getInt("total-messages", 0); //initialize to 0 if it doesn't exist
                editor.putInt("total-messages", totalMessages);
                editor.apply();
                }
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();

        unregisterReceiver(receiver);
    }
}

问题是广播接收器清除通知的实现在作业的生命周期内。JobIntentService的任务是显示通知并离开广播接收器。但是,当用户单击CLEAR from通知时,将广播挂起的意图,但是没有人收听它


作为解决方案,我建议您创建一个单独的广播接收器,并将其注册到
AndroidManifest.xml
中。到那时,您的广播将一直被收听,您可以执行其中的任何操作。

我们无法在Android 26中的AndroidManifest.xml中注册操作。@MuhammadMehdiRaza您确定吗?我们只能注册有限的操作,但不是全部。我还使用android manifest来注册操作,但在将目标sdk更新为26之后,现在我无法使用它们。OP涉及的是使用自定义操作注册广播,因此当然可以使用manifest注册。