关闭应用程序时,Android Push通知不起作用

时间:2019-04-17 04:00:05

标签: android push-notification android-8.0-oreo server-sent-events

我正在使用OkSse订阅我的服务器发送事件。

服务器每次发送新消息时,都会出现一条通知,告知应用程序处于前景,最小化还是完全关闭状态

最小化或处于前台状态时,通知会按预期工作,但是完全关闭时,此通知仅适用于某些设备品牌

研究bit时,我发现:

  

只有小米这样的制造商才注意到此问题,   Oppo,One Plus,Vivo,联想,华为,三星和其他一些人。

即使应用已关闭,for(i in 1:1000) { Ui <- rnorm(200,0,1) } 也会如何显示通知?

我不使用WhatsApp, Facebook, Slack, Gmail, etc..,只需订阅FCM

我也有清单:

Sse

我该如何实现?

更新

我设法通过使用<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="com.goparty.communication.NotificationsAlarmReceiver"> <intent-filter> <action android:name="android.media.action.DISPLAY_NOTIFICATION" /> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> IntentService使它工作,但是对于BroadcastReceiver版本,它崩溃了。

在更多reading之后,我发现在定位Oreo及更高版本时,JobIntentService替代了IntentService

Android O的过渡非常简单,但是再次,当我关闭应用程序时,我的解决方案无法正常工作。

即使关闭了应用程序,通知的全部目的也可以正常工作。

使用JobIntentService在后​​台进行工作是通过发送带有我的新服务意图的IntentService

我对broadcast做的事也很不顺利。

这是我的代码:

JobIntentService

通知接收者:

public class NotificationService extends JobIntentService {

    private static final int JOB_ID = 1;
    private NotificationManager notificationManager;

    public static void enqueueWork(Context context, Intent intent) {
        enqueueWork(context, NotificationService.class, JOB_ID, intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.logGoParty(getClass().getSimpleName() + "#onCreate");
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.logGoParty("Running in background.");
        sendBroadcast(new Intent(getApplicationContext(), NotificationService.class));
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Logger.logGoParty(getClass().getSimpleName() + "#onHandleWork");
        Logger.logGoParty("Sse initialized");

        Request request = new Request.Builder().url("the sse url").build();
        OkSse okSse = new OkSse();

        ServerSentEvent sse = okSse.newServerSentEvent(request, new ServerSentEvent.Listener() {
            @Override
            public void onOpen(ServerSentEvent sse, Response response) {
                Logger.logGoParty(response.toString());
            }

            @Override
            public void onMessage(ServerSentEvent sse, String id, String event, String message) {
                Logger.logGoParty("Sse#onMessage: " + message);
                try {
                    JSONObject promoJson = new JSONObject(message);

                    sendNotification(...);

                } catch (JSONException e) {
                    Logger.logGoParty("JSONException: " + e.toString());
                }
            }
                // ...
        });
    }

    private void sendNotification(String promoImageUrl, String notificationContent, String title) {

        try {
            URL url = new URL(promoImageUrl);
            Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());


            NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
            style.bigPicture(bitmap);

            Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Intent intent = new Intent(this, MapsActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("page-to-open", "promotions");
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, GoParty.PROMO_CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentTitle(title)
                    .setAutoCancel(true)
                    .setSound(defaultSound)
                    .setContentText(notificationContent)
                    .setContentIntent(pendingIntent)
                    .setStyle(style)
                    .setLargeIcon(bitmap)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE);

            notificationManager.notify(0, notificationBuilder.build());

            Log.i("GoParty", "Notification sent ----- ");

        } catch (MalformedURLException e) {
            Logger.logGoParty(e.toString());
        } catch (IOException e) {
            Logger.logGoParty(e.toString());
        }
    }
}

清单:

public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent notificationIntent = new Intent(context, NotificationService.class);
        NotificationService.enqueueWork(context, notificationIntent);
    }
}

2 个答案:

答案 0 :(得分:2)

这背后的主要原因是“自动启动”和“电池优化”(MIUI)设备尝试在“电池设置”中为您的应用更改“不优化”选项

 private void initOPPO() {
        try {

            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.floatwindow.FloatWindowListActivity"));
            startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
            try {

                Intent intent = new Intent("action.coloros.safecenter.FloatWindowListActivity");
                intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"));
                startActivity(intent);
            } catch (Exception ee) {

                ee.printStackTrace();
                try {

                    Intent i = new Intent("com.coloros.safecenter");
                    i.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity"));
                    startActivity(i);
                } catch (Exception e1) {

                    e1.printStackTrace();
                }
            }

        }
    }

    private static void autoLaunchVivo(Context context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.iqoo.secure",
                    "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                        "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                context.startActivity(intent);
            } catch (Exception ex) {
                try {
                    Intent intent = new Intent();
                    intent.setClassName("com.iqoo.secure",
                            "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                    context.startActivity(intent);
                } catch (Exception exx) {
                    ex.printStackTrace();
                }
            }
        }
    }

用法

if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
                                initOPPO();
                            } else if (Build.MANUFACTURER.equalsIgnoreCase("vivo")) {
                                autoLaunchVivo(BaseSettingActivity.this);
                            } else if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
                                try {
                                    Intent intent = new Intent();
                                    intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                                    startActivity(intent);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

答案 1 :(得分:1)

您的方法:使用OkSse进行通知意味着需要一直运行的后台服务。

问题1:Android后台服务限制link

JobIntentService在内部将使用JobService,并且不会继续运行。它不会无差别地运行,因为将适用执行时间限制。它将停止并重新计划以在以后继续执行。因此,在您的情况下,它将不起作用。

问题2:设备小米,Oppo,One Plus,Vivo,联想,华为,三星的电池优化检查使JobIntentService停止。

解决方案:

1)运行不建议使用的丑陋的前台服务

2)强烈建议Firebase高优先级通知link

我已经在上述设备上实现了该功能,即使该应用设置为电池优化模式,您也会收到通知。