Notification in Oreo working sometimes and sometimes not

39
March 16, 2019, at 10:30 AM

I am working with notification in Android app

The notification is working perfectly in any version under O.

In Oreo its working (Showing/Pushing) sometimes and sometimes not, When I close app completely its work and also if I locked the device... but not more than 5 minutes after closing.

I do every things I saw.. I put permissions:

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

and I work with JobIntentService

also I checked for SDK VERSION after build notification...

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 

also I set Notification Channel:

NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

but every that is not changing any thing...

I don't know what should I do. any help please, and Thanks.

my code:

NotificationEventReceiver.class

package com.example.android.homepharmacy.broadcast_receivers; 
 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.content.WakefulBroadcastReceiver; 
import android.util.Log; 
 
import com.example.android.homepharmacy.notifications.NotificationIntentService; 
 
import java.util.Calendar; 
import java.util.Date; 
 
 
/** 
 * WakefulBroadcastReceiver used to receive intents fired from the AlarmManager for showing notifications 
 * and from the notification itself if it is deleted. 
 */ 
public class NotificationEventReceiver extends WakefulBroadcastReceiver { 
 
    private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE"; 
    private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; 
 
 
    public static void setupAlarm(Context context) { 
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
        PendingIntent alarmIntent = getStartPendingIntent(context); 
 
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                getTriggerAt(new Date()), 
                60000 , 
                alarmIntent); 
    } 
 
 
    public static void cancelAlarm(Context context) { 
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
        PendingIntent alarmIntent = getStartPendingIntent(context); 
        alarmManager.cancel(alarmIntent); 
    } 
 
    private static long getTriggerAt(Date now) { 
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(now); 
        return calendar.getTimeInMillis(); 
    } 
 
    private static PendingIntent getStartPendingIntent(Context context) { 
        Intent intent = new Intent(context, NotificationEventReceiver.class); 
        intent.setAction(ACTION_START_NOTIFICATION_SERVICE); 
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 
 
    public static PendingIntent getDeleteIntent(Context context) { 
        Intent intent = new Intent(context, NotificationEventReceiver.class); 
        intent.setAction(ACTION_DELETE_NOTIFICATION); 
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
                String action = intent.getAction(); 
                Intent serviceIntent = null; 
                if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) { 
                    Log.i(getClass().getSimpleName(), "onReceive from alarm, starting notification service"); 
                    serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); 
                } else if (ACTION_DELETE_NOTIFICATION.equals(action)) { 
                    Log.i(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete"); 
                    serviceIntent = NotificationIntentService.createIntentDeleteNotification(context); 
                } 
 
                if (serviceIntent != null) { 
                    NotificationIntentService.startService(context); 
                } 
    } 
}

NotificationIntentService Methods:

private void processStartNotification() { 
        // Do something. For example, fetch fresh data from backend to create a rich notification? 
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); 
             
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(context , CHANNEL_ID); 
            builder.setContentTitle("Drug Reminder for: " + memName) 
                    .setAutoCancel(true) 
                    .setSmallIcon(R.drawable.drug_icon) 
                    .setColor(getResources().getColor(R.color.colorAccent)) 
                    .setContentText("Now its time for " + memName + 
                            " to get " + drugName) 
                    .setSmallIcon(R.drawable.logo) 
                    .setChannelId(CHANNEL_ID); 
 
            Intent mainIntent = new Intent(this, CourseActivity.class); 
            mainIntent.putExtra(Intent.EXTRA_TEXT, courseId); 
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 
                    NOTIFICATION_ID, 
                    mainIntent, 
                    PendingIntent.FLAG_UPDATE_CURRENT); 
            builder.setContentIntent(pendingIntent); 
            builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this)); 
 
 
            NotificationManager mNotificationManager = 
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
            /* Create or update. */ 
            NotificationChannel channel = new NotificationChannel("my_channel_01", 
                    "Channel human readable title", 
                    NotificationManager.IMPORTANCE_DEFAULT); 
            // mNotificationManager.createNotificationChannel(channel); 
            mNotificationManager.createNotificationChannel(mChannel); 
            mNotificationManager.notify(NOTIFICATION_ID, builder.build()); 
 
 
 
        } 
        else { 
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
            builder.setContentTitle("Drug Reminder for: " + memName) 
                    .setAutoCancel(true) 
                    .setSmallIcon(R.drawable.drug_icon) 
                    .setColor(getResources().getColor(R.color.colorAccent)) 
                    .setContentText("Now its time for " + memName + 
                            " to get " + drugName) 
                    .setSmallIcon(R.drawable.logo); 
 
            Intent mainIntent = new Intent(this, CourseActivity.class); 
            mainIntent.putExtra(Intent.EXTRA_TEXT, courseId); 
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 
                    NOTIFICATION_ID, 
                    mainIntent, 
                    PendingIntent.FLAG_UPDATE_CURRENT); 
            builder.setContentIntent(pendingIntent); 
            builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this)); 
 
 
            NotificationManagerCompat manager = NotificationManagerCompat.from(this); 
            manager.notify(NOTIFICATION_ID, builder.build()); 
 
        } 
 
 
    }

READ ALSO
How to adjust the text auto-resize &amp; when it moves to the 2nd line

How to adjust the text auto-resize & when it moves to the 2nd line

I am building a meme generatorThe issue that I'm having is with the texts that are drawn on the Canvas

22
How can get android.developer site offline? [on hold]

How can get android.developer site offline? [on hold]

I want to download all documentation of this site: https://developerandroid

17
When a WindowManager android.view.WindowLeaked is thrown, is memory recovered?

When a WindowManager android.view.WindowLeaked is thrown, is memory recovered?

In certain rare situations, I get an error like E/WindowManager: androidview

21