What is the best way to deliver notification daily

2019-09-09 13:45发布

I wanted to deliver daily notification at a specified time, I did that but when I close the app from recents, the notification did not delivered at all. I dont know how to figure that out. Can someone please suggest me what is the best way to deliver the notification even if the app is closed (not force closed) or the device is booted, the notification must be at the exact time.

Any help would be appreciated.

Here is the code,

The alarmreciever class,

public class AlarmReciever extends BroadcastReceiver {

String meaning;
String word;

Context context;

@Override
public void onReceive(Context context, Intent intent) {

 this.context=context;
    FileIO fileIO=new FileIO(context);

    String res=fileIO.readFile("daily_word");
    StringTokenizer stringTokenizer = new StringTokenizer(res,"---");
   word = stringTokenizer.nextToken();
    meaning = stringTokenizer.nextToken();

    fileIO.writeFile(word+"---"+meaning,"sent_word");

    int ch= word.charAt(0);
    if(ch>=65 && ch<=90)
    {
    }
    else
        ch=ch-32;

    word=(char)ch+word.substring(1);

    produceNotificatioon(context);

    System.out.println("<> ALARM RECEiVE");



}




public void produceNotificatioon(Context context)
{
    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(context);
    notificationBuilder.setAutoCancel(true)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setDefaults(Notification.DEFAULT_LIGHTS)
            .setContentTitle("Word of the Day")
            .setContentText("Today's Word is "+word)
            .setSmallIcon(R.drawable.app_icon_2);

    Intent myintent = new Intent(context,WordOfTheDay.class);

    TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(context);

    taskStackBuilder.addParentStack(WordOfTheDay.class);
    taskStackBuilder.addNextIntent(myintent);



    PendingIntent pendingIntent=taskStackBuilder.getPendingIntent(102,PendingIntent.FLAG_UPDATE_CURRENT);

    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());


}

}

The setAlarm method,

public void setAlarm()
{
    Intent myintent=new Intent(this,AlarmReciever.class);


    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


    PendingIntent pendingIntent=PendingIntent.getBroadcast(this,101,myintent,PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.cancel(pendingIntent);
    Calendar calendar=Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, NotificationTime.getInt("hour", 12));
    calendar.set(Calendar.MINUTE, NotificationTime.getInt("min", 0));
    calendar.set(Calendar.SECOND, 0);


    if(calendar.before(Calendar.getInstance()))
        calendar.add(Calendar.DATE,1);


    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY
            , pendingIntent);


    System.out.println("ALARM SET STATUS");
}

Below is some of the oncreate code,

            if(checkFirstRun()) {

            createAlert createAlert=new createAlert(this);
            createAlert.createDialog();



            setDaily_word();

            setAlarm();
            System.out.println("ALARM SET STATUS on FIRST RUN");
        }

I am setting alarm if it is the apps first RUn, Otherwise I am setting alarm either using timepicker of using the service onCreate() method

Here is the service class,

public class ServiceClass extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {


    System.out.println("class Service started");
}

@Override
public void onTaskRemoved(Intent rootIntent) {

    System.out.println("class Service on TaskRemoved");
    //   setAlarm();

    //  onCreate();
    System.out.println("class Service on Alarm reset  oncreate");
    super.onTaskRemoved(rootIntent);
}

@Override
public void onCreate() {

    System.out.println("class Service created");
   setAlarm();   // this work if I start my application just after it is being cleared from recent apps
    super.onCreate();
}
}

here is my manifest file, I am using only these permissions

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
.....
    <receiver android:name=".AlarmReciever" android:process=":remote"/>
    <service android:name=".ServiceClass"/>

1条回答
三岁会撩人
2楼-- · 2019-09-09 14:16
  1. If notification is FCM/GCM it would range between 0-15 min, the nature of the push delivery cannot guarantee exact timing.

  2. If notification is produced locally by your app at some point, you can use AlarmManager to schedule an intent to start BroadcastReceiver to trigger creation of notification.

查看更多
登录 后发表回答