AlarmManager在Android 6.0的BroadcastReceiver中不会触发onReceive

时间:2018-12-14 15:08:23

标签: android notifications broadcastreceiver alarmmanager

我尝试使用AlarmManager和Broadcast Receiver关闭通知,但是从不调用onReceive方法,我想知道为什么吗?我已经搜索了几乎所有相关主题,但是没有一个可以帮助我。这是我的代码:

public class NotificationCancelReceiver extends BroadcastReceiver {
    @Inject
    protected NotificationManager notificationManager;

    public NotificationCancelReceiver() {
        super();
        App.appComponent.inject(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String id = intent.getStringExtra(OtherParams.NOTIFICATION_ID);
        if(!id.isEmpty()){
            notificationManager.cancelNotification(id);
        }
    }
}


Here is the code reponsible for creating the alarm:


    private void setCancelAlarm(String notificationId){
       AlarmManager alarmManager = (AlarmManager) application.getApplicationContext().getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(application, NotificationCancelReceiver.class);
        intent.putExtra(OtherParams.NOTIFICATION_ID, notificationId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
               application, Integer.parseInt(notificationId), intent, 0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,
                SystemClock.elapsedRealtime() + 10, pendingIntent);
    }

Manifest.xml:

<receiver
    android:name="services.NotificationCancelReceiver">
</receiver>

我还用Dagger注入了广播接收器,但是即使禁用了它也不会触发:

@Singleton
@Component(modules = {
        AppModule.class})
    public interface AppComponent {
        ActivityComponent getActivityComponent(ActivityModule module);

        void inject(App app);

        void inject(NotificationCancelReceiver notificationReceiver);
    }

0 个答案:

没有答案