即使应用被终止,每天发送通知的最佳方法是什么 [英] What is the best way to deliver notification daily even if the app is killed

查看:103
本文介绍了即使应用被终止,每天发送通知的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在指定的时间发送每日通知,但我做到了,但是当我关闭最近的应用程序时,通知根本没有发送.我不知道该怎么办.有人可以建议我什么是发送通知的最佳方法,即使该应用已关闭(不是强制关闭)或设备已启动,该通知也必须在正确的时间.

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.

任何帮助将不胜感激.

这是代码,

警报接收器类,

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方法,

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

下面是一些oncreate代码,

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

如果它是应用程序的第一个RUn,我将设置警报,否则,我将使用使用服务onCreate()方法的时间选择器来设置警报

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

这是服务类别,

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. 如果通知为 FCM/GCM ,则通知时间为0-15分钟,推送交付的性质不能保证确切的时间安排.

  1. If notification is FCM/GCM it would range between 0-15 min, the nature of the push delivery cannot guarantee exact timing.

如果您的应用有时在本地生成通知,则可以使用创建通知.

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.

这篇关于即使应用被终止,每天发送通知的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆