即使应用程序被杀,每天发送通知的最佳方式是什么

时间:2016-06-16 17:27:01

标签: android notifications repeatingalarm

我想在指定时间发送每日通知,我这样做但是当我从最近关闭应用时,通知根本没有发送。我不知道如何解决这个问题。有人可以建议我,即使应用程序已关闭(非强行关闭)或设备启动,提供通知的最佳方式是什么,通知必须在确切的时间。

任何帮助都将不胜感激。

这是代码,

警报器类,

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());


}

}

setAlarm方法,

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");
}

下面是一些oncreate代码,

            if(checkFirstRun()) {

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



            setDaily_word();

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

我正在设置闹钟,如果它是应用程序第一个RUn,否则我使用timeCicker使用服务onCreate()方法设置闹钟

这是服务类

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();
}
}

这是我的清单文件, 我只使用这些权限

     <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 个答案:

答案 0 :(得分:1)

  1. 如果通知为FCM/GCM,则范围介于0-15分钟之间,推送的性质无法保证准确的时间。

  2. 如果您的应用在某个时间点在本地生成通知,则可以使用AlarmManager安排an intent to start BroadcastReceiver来触发creation of notification