Не приходят уведомления Android

276
24 июля 2017, 13:12

Здравствуйте. Пытаюсь учить разработку приложений под Android устройства. Уже некоторое время разрабатываю приложение в котором есть функция отправки уведомлений (напоминаний) в разное время суток. Но если телефон заблокирован и время до отправки уведомление больше 10 минут то они не приходят. Использую BroadcasrReceiver:

public class NotificationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    addNotif(context, "Times UP Receiver", "5 second Receiver", "Alert Receiver");
}
private void addNotif(Context context, String msg, String msgText, String msgAlert) {
    Notification.Builder builder = new Notification.Builder(context);
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent)
            .setSmallIcon(R.drawable.ic_android_black_24dp)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp))
            .setTicker(msgAlert)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(msg)
            .setContentText(msgText);
    Notification notification = builder.build();
    notification.defaults = Notification.DEFAULT_ALL;
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(101, notification);
}
}

используя AlarmManager:

        Calendar calNotif = Calendar.getInstance();
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 102, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (15 * 60 * 1000), pendingIntent);

Пробовал также использовать Service:

public class NotificationService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Calendar calNotif = Calendar.getInstance();
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 103, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (16 * 60 * 1000), pendingIntent);
    return START_STICKY;
}
}

Manifest:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".NotificationReceiver" />
    <service android:name=".NotificationService" />

но ничего не работает. Уже голова болит, все никак не могу понять почему уведомления не приходят. Подскажите в чем проблема.

READ ALSO
java sqlite PreparedStatement не возвращает результат

java sqlite PreparedStatement не возвращает результат

Не происходит вход в тело цикла while при использовании первого запроса (rs: колонок 2, строк 0)Оба запроса рабочие, проверены в SQLiteStudio

258
Не работает валидация формы на jquery

Не работает валидация формы на jquery

Не работает валидация формы на jqueryНе могу понять почему

280
Как сделать так, чтобы фон сужался только справа и слева?

Как сделать так, чтобы фон сужался только справа и слева?

Всем привет! Есть вот такая секция на сайтеНадо чтобы при изменении размера экрана/браузера(то есть при адаптиве), фон сужался только слева...

259