ViLu ViLu - 5 months ago 230
Android Question

Notification not working on API 23

my notification is not working on API 23.

My Notfication works succesfull from API 16 to 22

The 100 for calstd/calmin is a default number.

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
if(calstd==100||calmin==100){
cancelAlarm();
}
else {
onTimeSet(calstd, calmin);
}


.

pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, mainsite.class), 0);
nm1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notif = new Notification.Builder(context);
uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

notif.setContentTitle("Remind Me");
notif.setContentText("Vergiss deine Pille nicht :)");
notif.setSmallIcon(R.drawable.ic_launcher);
notif.setSound(uri);
notif.setAutoCancel(true);
notif.setContentIntent(pendingIntent);
nm1.notify(15, notif.build());


.

public void setAlarm(Calendar targetCal) {

intent = new Intent(contexta, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(contexta, RQS_1, intent, 0);

alarmManager = (AlarmManager) contexta.getSystemService(Context.ALARM_SERVICE);

if (Build.VERSION.SDK_INT >= 19)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
else if (Build.VERSION.SDK_INT >= 16)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1 * 1000 * 60 * 60 * 24, pendingIntent);
}


Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:process=":remote" android:name=".Alarm"/>

<receiver android:name="com.victoriaremindme.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>


Its not working after closing the Apps. I cant find the Problem.

Answer

For API 23 and above you can use setAndAllowWhileIdle()

So change your code to

    if (Build.VERSION.SDK_INT >= 23)
            alarmManager. setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

   else if (Build.VERSION.SDK_INT >= 19)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

   else if (Build.VERSION.SDK_INT >= 16)
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1 * 1000 * 60 * 60 * 24, pendingIntent);

Check the documentation here.

Hope this helps.

Comments