通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)


問題描述

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

我有一個無法解決的問題,我今天花了 7 個小時試圖找到解決方案。我正在嘗試為我的應用顯示通知。它在具有舊操作系統版本的舊設備上運行良好,但是在運行 Oreo API 27 的 Note 8 上,我的通知沒有顯示。我花了幾個小時的測試,我發現了問題:當應用程序正在運行並且應用程序打開時,會顯示通知,但是當我退出應用程序時,通知不會顯示。所以我認為這與系統處理服務的方式有關。

這是正常行為嗎?有沒有辦法解決這個問題?

這是我的通知代碼:

onCreate...
 if (settings.getBoolean("enabled", true)) {
            if (settings.getLong("lastRun", Long.MAX_VALUE) < System.currentTimeMillis() ‑ mTimeDelay) {
                sendNotification();
            }
        }

        setAlarm();
        stopSelf();
}
  public void setAlarm() {

        Intent serviceIntent = new Intent(this, CheckRecentRun.class);
        PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + mTimeDelay, pi);
        //Alarm is set for 3 days
    }

    public void sendNotification() {
        Intent intent = new Intent(this, WelcomeActivity.class);
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);



        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = notificationManager.getNotificationChannel(channelId);
            if (mChannel == null) {
                mChannel = new NotificationChannel(channelId, channelName, importance);
                mChannel.setDescription(mNotificationText);
                mChannel.enableLights(false);
                mChannel.setLightColor(Color.GREEN);
                mChannel.setShowBadge(false);
                notificationManager.createNotificationChannel(mChannel);
            }
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId).setStyle(new NotificationCompat.BigTextStyle().bigText(mNotificationText)).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(mAppName).setContentText(mNotificationText).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(mNotificationCode, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify(notificationId, mBuilder.build());
    }

參考解法

方法 1:

It may be Samsung's aggressive battery management behaviour (among many other brands like Huawei) on their newer phones killing your app.

Check out this website: https://dontkillmyapp.com/

And look at the notes under Samsung:

Galaxy S8 and later With the introduction of their flagship Galaxy S8 (and with some earlier experiments), Samsung has introduced a flawed attempt at prolonging battery life called App power monitor.

For your apps to work correctly, please whitelist them in App power monitor.

How to do it:

Open the Settings > Device maintenance > Battery and at the bottom you’ll see a list of your most frequently used apps. You can manage apps individually or in a group by selecting them then tapping the big Save power button. Apps that are sleeping will appear in the Sleeping apps list at the bottom (tap it to expand the list). Scrolling further — all the way to the very bottom — and you’ll find Unmonitored apps. These are apps that you specifically want to exclude (white list) from App power monitor evil reach.

When inside the Unmonitored apps menu, you can tap the 3‑dot menu to add or delete apps from the list. Rather than bothering with any of that, you can just turn off the App power monitor feature completely as it has little‑to‑no impact on battery life and only serves to handicap the normal functioning of your Galaxy phone.

It’s excessive and in some cases downright misleading, using scare tactics to keep you reliant on Samsung’s software when other Android devices get by just fine without it.

Try this first.. Let me know if there is still any issue after this.

If your code works on other devices or on your emulator, this should be the issue.

(by Slim C.H Wong)

參考文件

  1. Notification not showing because service or alarm manager is killed off (CC BY‑SA 2.5/3.0/4.0)

#system #notifications #service #Android #alarmmanager






相關問題

asp.net c# sistem login (asp.net c# login system)

系統函數的彙編代碼(iPhone) (Assembly code to system functions (iPhone))

如何跟踪系統依賴關係? (How to track System Dependencies?)

使用 system() 甚至 passthru() 從 php 運行 python 腳本不會產生任何輸出。為什麼? (Running a python script from php with system( ) or even passthru( ) produces no output. Why?)

求解線性方程 (Solving a linear equation)

C++ 中的 System() 調用及其在編程中的作用 (System() calls in C++ and their roles in programming)

找不到傳遞給使用 Perl“系統”命令調用的程序的參數 (Cannot find argument passed to program called using Perl "system" command)

如何顯示所有用戶表中的所有列/字段? (How to display all columns/fields in all user tables?)

系統文件夾中的庫未加載正確的語言 (Libraries from system folder does not load correct language)

為什麼系統不返回主值? (Why system doesn't return main's value?)

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

通過 C++ 調用系統不一致失敗 (Calling the system through C++ inconsistently fails)







留言討論