APP在关闭程序和手机锁屏后保持接收推送

( 最近做的一个项目,记录一下处理的过程)
需求是需要提醒用户后台已经下发了任务,及时查看。我是用的极光做的别名推送到客户端接收(因为免费)。but理想很美好,客户反馈根本收不到消息,他们平常工作中的时候基本上就不会时不时的打开这个app一下,导致了应用长时间不用就被杀死了。能怎么办呢,客户就是上帝啊,开始研究吧!

本着程序中的问题程序中去解决,开始从网上搜罗,试了以下几种方法

1、把接收极光推送的Receiver的优先级调高
    <receiver
        android:name="com.neuqsoft.app.jpush.PushMessageReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter android:priority="1000">
            <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>

这种方法真的有用吗?
priority的作用是:有序广播发出的情况下,多个receiver满足条件的情况下,优先触发priority高的recevier。
保持怀疑的态度试了下…果然并没有用。

2、设置android:persistent=“true”,伪装成系统应用保活
  在AndroidManifest.xml 的application标签中设置 android:persistent="true

测试了下小米手机,android9.0的系统,并没有用。伪装成系统应用,不仅仅要设置android:persistent="true,跟程序的预装位置也有关系。所以这个方法也是行不通的。

3、设置app允许应用通知,允许程序自启动
public static boolean isNotificationEnabled(Context context) {
    AppOpsManager mAppOps =
            (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

    ApplicationInfo appInfo = context.getApplicationInfo();
    String pkg = context.getApplicationContext().getPackageName();
    int uid = appInfo.uid;
    Class appOpsClass = null;

    /* Context.APP_OPS_MANAGER */
    try {
        appOpsClass = Class.forName(AppOpsManager.class.getName());

        Method checkOpNoThrowMethod =
                appOpsClass.getMethod(CHECK_OP_NO_THROW,
                        Integer.TYPE, Integer.TYPE, String.class);

        Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
        int value = (Integer) opPostNotificationValue.get(Integer.class);

        return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
                AppOpsManager.MODE_ALLOWED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}


public static void toNotificationSetting(Context context) {
    Intent localIntent = new Intent();
    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= 9) {
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
    } else if (Build.VERSION.SDK_INT <= 8) {
        localIntent.setAction(Intent.ACTION_VIEW);

        localIntent.setClassName("com.android.settings",
                "com.android.settings.InstalledAppDetails");

        localIntent.putExtra("com.android.settings.ApplicationPkgName",
                context.getPackageName());
    }
    context.startActivity(localIntent);

}

手机的通知权限是必须要开启的,把优先级调到最高,允许提示音震动。开启应用的自启权限,是没有固定的方法的,需要用户去主动设置一下。

4、忽略手机电池优化
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(getPackageName());

            if (!hasIgnored) {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
    }
    
 <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

如果没有给到这个权限的话会弹出一个提示框让用户去设置。用过之后是有效的。可以避免程序在手机锁屏时迅速的被杀死。从而增加程序的存活时间。

总结

第一和第二点是没有效果的。第三点和第四点有效果。第三点收通知权限是必须要开启的,否则无法接收推送。使用第四点能保证程序在后台运行时不被迅速杀死,可以接收到推送消息。

参考: Android:判断应用程序接收通知开关是否打开
有写的不对的地方请大家指正,谢谢

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值