简体   繁体   English

应用终止后未触发Android通知

[英]Android notification not fired when app killed

I am using alarm manager to set the time and fire a notification at that particular time. 我正在使用警报管理器设置时间并在该特定时间触发通知。 Works fine when app is foreground or background. 当应用程序是前景或背景时,效​​果很好。 I am testing on Android 8. However my problem is when app is killed from task manager, the notification is not fired because the broadcast does not work. 我正在Android 8上进行测试。但是,我的问题是,当应用程序从任务管理器中终止时,由于广播无法正常工作,因此未触发通知。 This is because I register receiver in onCreate() of MainActivity and unregister receiver in onDestroy(). 这是因为我在MainActivity的onCreate()中注册了接收者,而在onDestroy()中取消了注册接收者。 Maybe it would work if I register my broadcast receiver in manifest but it is not allowed from Android 8 onwards. 如果我在清单中注册我的广播接收器,也许会工作,但是从Android 8开始不允许这样做。 How do I address this problem that I receive the broadcast even when app is killed? 我如何解决即使应用被杀死我仍然收到广播的问题?

BroadCastREceiver Example BroadCastREceiver示例

 public class SchedulerMailBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { try { Log.i("scheduler: " + new DateWrapper().getLocaleString()); PowerManager pm = (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = null; if (pm != null) { Log.d("aquire wakelog"); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ics:mailScheduler"); wl.acquire(10000); } startInNewThread(context.getApplicationContext(),wl); }catch(Exception e){ Log.e(e); } } public void startInNewThread(final Context context, PowerManager.WakeLock wl){ new ThreadWrapper(() -> { try { //Do Stuff }catch(Exception e){ Log.e(e); }finally { Log.d("releasing wakelog"); try { wl.release(); Log.i("released wakelog"); }catch(Exception e){ Log.e("wakelog release","exception on releasing wawkelog"); } } }).start(); } public static boolean registerScheduler(Context context){ final int requestCode=1234; Intent intent = new Intent(context, SchedulerMailBroadcastReceiver.class); intent.setAction("startScheduler"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE); if (alarmManager != null) { // alarmManager.cancel(previousPendingIntent); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 60*15, pendingIntent); Log.i("registered repeating"); } return true; } } 

you probably dont want setRepeating though 你可能不想setRepeating

JobService example JobService示例

 public class SchedulerMailJobManager extends JobService{ @Override public boolean onStartJob(JobParameters params) { try { Log.i("SchedulerMailJobManager","scheduler run at "+ new DateWrapper().getLocaleString()); startInNewThread(getApplicationContext(),params); return true; }catch(Exception e){ Log.e("SchedulerMailJobManager","errpr in starting new thread",e); return false; } } @Override public void onDestroy(){ Intent intent = new Intent(this,ServiceRestarter.class); intent.setAction("restartService"); intent.putExtra("service","mailScheduler"); sendBroadcast(intent); } @Override public boolean onStopJob(JobParameters params) { Log.w("stoppedJob","stopped"); return true; } public void startInNewThread(final Context context,final JobParameters params){ new ThreadWrapper(() -> { try { //Do Stuff }catch(Exception e){ Log.e("JobScheduler2ndThread","Exception",e); }finally { if(params!=null) { this.jobFinished(params,false); }else{ Log.e("JobScheduler2ndThread","no params for jobFinished"); } } }).start(); } static JobInfo createScheduledJob(Context context){ ComponentName serviceComponent = new ComponentName(context,SchedulerMailJobManager.class ); JobInfo.Builder builder=new JobInfo.Builder(1234,serviceComponent); int waitMin=30; builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); builder.setPersisted(true); builder.setPeriodic(1000*60*waitMin,1000); return builder.build(); } public static boolean registerScheduler(Context context){ JobScheduler scheduler=context.getSystemService(JobScheduler.class); if(scheduler==null){ Log.e("registerScheduler","scheduler is null"); return false; } if(scheduler.getPendingJob(1234)!=null) { scheduler.cancel(1234); Log.i("registerScheduler","cancelled previous"); } int resultCode=scheduler.schedule(createScheduledJob(context)); if(resultCode==JobScheduler.RESULT_SUCCESS){ Log.i("JobManagerScheduler","registered new scheduler"); return true; }else{ Log.e("registerScheduler","failed registering"); return false; } } } 

since you only have a Notification you probably dont need to start anything in a new thread which means in this solution you should return false in OnStartJob 因为您只有一个Notification,所以您可能不需要在新线程中启动任何东西,这意味着在此解决方案中,您应该在OnStartJob中返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序被终止时,通知未在 Android 8 及更高版本中显示 - Notification not showing in Android 8 and above when app is killed 应用程序被杀死时没有收到通知 - Not receiving notification when the app is killed 在Android上杀死应用时收到系统托盘通知 - Receive system tray notification when app killed on Android 应用程序关闭或被杀死时未收到 Android FCM 通知 - Android FCM notification is not received when app is close or Killed 即使应用程序被杀死,如何向Android设备发送推送通知? - How to send push notification to android devices even when app is killed? 当应用程序被杀死时,Flutter android 不会收到通知 - Flutter android do not get notification when app is killed 当应用程序被杀死时,Android 通知操作按钮不起作用 - Android notification action buttons are not working when app is killed 当应用程序被杀死时,Android 中的长通知标题会被裁剪 - Long Notification Title in Android gets cropped when the app is killed 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM 在Android设置应用程序图标时的应用程序图标上的徽章编号或在接收Facebook应用程序等通知时被杀死 - On Android setting badge number on app icon when app is in background or killed when receiving notification like facebook app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM