Android-当应用被杀死时,不会生成通知

时间:2019-09-05 22:21:21

标签: android broadcastreceiver android-notifications

我创建了一个警报管理器以及一个通知。除非通过任务管理器(三星或华为)将应用杀死,否则一切都会正常运行。

通过许多线程,我已经发现一些可能的问题:

  1. 一旦用户终止应用程序(刷卡),某些制造商就会终止服务。
  2. 大量电池优化会损害执行广播的接收器
  3. 如果在主线程中为后台服务编码

所以我到目前为止所做的:

  1. 添加了完整的唤醒锁
  2. 添加了权限,例如忽略电池优化(使用受到playstore的限制)
  3. 为不同的SDK添加了alarmmanager设置
  4. 最终在智能手机(Galaxy S9,华为P30 Pro)的设置上将应用列入白名单

public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        MyWakeLock.acquire(context);

        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());

        MyWakeLock.release();
    }
}

public class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";

    private NotificationManager mManager;

    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

        getManager().createNotificationChannel(channel);
    }

    public NotificationManager getManager() {

        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification() {

            return new NotificationCompat.Builder(getApplicationContext(), channelID)
                    .setContentTitle(getString(R.string.notification_title))
                    .setContentText(getString(R.string.notification_text_noEvents))
                    .setStyle(new NotificationCompat.InboxStyle()
                            .addLine(getString(R.string.notification_text_noEvents)))
                    .setSmallIcon(R.drawable.round_today_24);

        }
    }
}


public abstract class MyWakeLock {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context c) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyApp:MyLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null){
            wakeLock.release();
        }
        wakeLock = null;
    }
}


    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".AlertReceiver" />

    </application>

</manifest>

通过adb,我发现广播接收器没有(应该)被杀死,因为在终止该应用程序之后,该服务仍处于活动状态,并且广播接收器(Log.i)中的日志显示出来。但是不知何故,这两行没有执行:

NotificationCompat.Builder nb = notificationHelper.getChannelNotification(); notificationHelper.getManager()。notify(1,nb.build());

这也是电池优化问题吗?还是主用户界面中的某项阻止了此操作?

0 个答案:

没有答案