Home » Android » timed notifications not firing in android 6 and above

timed notifications not firing in android 6 and above

Posted by: admin November 1, 2017 Leave a comment

Questions:

i am making app that fires notifications in specific time every day
the problem the notifications fire normal in android 5 , but in higher versions like 6 or above , it doesn’t fire

this is my activity

 calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, new_Hour);
            calendar.set(Calendar.MINUTE, new_Minute);
            calendar.set(Calendar.SECOND,0);

            system_time=Calendar.getInstance();
            system_time.setTimeInMillis(System.currentTimeMillis());
            long diff1 =  system_time.getTimeInMillis() - calendar.getTimeInMillis() ;

            if (diff1 > 0){
                calendar.add(Calendar.DATE,1);
                calendar.set(Calendar.HOUR_OF_DAY, new_Hour);
                calendar.set(Calendar.MINUTE, new_Minute);
                calendar.set(Calendar.SECOND,0);

            }

            intent_wake = new Intent(AlarmsActivity.this, AlarmReciever.class);
            if(lang.equals("ara")){intent_wake.putExtra("id",id);}
            else if (lang.equals("eng")){intent_wake.putExtra("id",11);}
            p_intent_wake = PendingIntent.getBroadcast(AlarmsActivity.this, id , intent_wake, PendingIntent.FLAG_UPDATE_CURRENT);
            manager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),24 * 60 * 60 * 1000, p_intent_wake);

and my reciever class is

 NotificationCompat.Builder notification;

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, MainAzkarActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);

        switch (id) {

            case 1:


                notification = new NotificationCompat.Builder(context)
                        .setContentTitle("Title")
                        .setContentText("message")
                        .setVisibility(Notification.DEFAULT_LIGHTS)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(bitmap)
                        .setAutoCancel(true)
                        .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ringtone))
                        .setVibrate(new long[]{1000, 500, 1000, 500, 1000, 500, 1000})
                        .setContentIntent(PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));
                //    .build();

                nm.notify(0, notification.build());
                break;

and my manifest

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>

and reciever

 <receiver android:name="com.azkar.mohamed.zakrny.AlarmReciever"

        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </receiver>
Answers: