如何解决-如果一段时间未打开应用,Onesignal推送通知将不会进入Android设备?

问题描述

在我的Android应用程序中,我正在使用一个信号推送通知服务来发送推送通知。我已经按照提到的文档完成了所有设置。

设置完所有这些内容之后,我创建了一个信号如下的服务类,如下所示-

NotificationExtenderBareBonesExample.java

public class NotificationExtenderBareBonesExample extends NotificationExtenderService {

    public static String first_screen;

    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {

        SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences("NOTIFY_PREF",MODE_PRIVATE).edit();
        editor.putString("notify_msg",receivedResult.payload.groupMessage);
        editor.putString("notify_key",receivedResult.payload.groupKey);
        editor.apply();

        first_screen = receivedResult.payload.groupMessage;

        return false;
    }
}

我还创建了另一个类来处理收到的推送通知,如下所示-

ExampleNotificationReceivedHandler.java

public class ExampleNotificationReceivedHandler implements Onesignal.NotificationReceivedHandler {
    @Override
    public void notificationReceived(OSNotification notification) {
        JSONObject data = notification.payload.additionalData;
        String customKey;

        if (data != null) {
            customKey = data.optString("customkey",null);
            if (customKey != null)
                Log.i("OnesignalExample","customkey set with value: " + customKey);
        }
    }
}

然后,在我的Activity类中,我像下面一样初始化了一个信号-

Onesignal.startinit(this)
                .inFocusdisplaying(Onesignal.OSInFocusdisplayOption.Notification)
                .unsubscribeWhenNotificationsAredisabled(true)
                .setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
                .setNotificationopenedHandler(new MyNotificationopenedHandler(this))
                .init();

最后,在我的 AndroidManifest 文件中,我像下面这样声明了该服务-

<service
            android:name="com.rokomari.new_package.notification_check.NotificationExtenderBareBonesExample"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE">
            <intent-filter>
                <action android:name="com.onesignal.NotificationExtender" />
            </intent-filter>
        </service>

如果最近正在使用该应用程序,则推送通知即将到来,但是问题仍然存在,正如我在问题中提到的那样。因此,我检查了一些其他解决方案,并在我的项目中将其应用如下-

我使我的应用程序成为系统应用程序,并为此添加了一项身份验证服务。 我还添加一个broadcast-Receiver类,如下-

BootReceiver.java

public class BootReceiver extends broadcastReceiver {

    @Override
    public void onReceive(Context context,Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

            Intent startServiceIntent = new Intent(context,NotificationExtenderBareBonesExample.class);
            context.startService(startServiceIntent);

            Intent notificationServiceIntent = new Intent(context,NotificationExtenderBareBonesExample.class);
            context.startService(notificationServiceIntent);
        }
    }
}

并在我的 AndroidManifest 文件中声明了这一点-

<receiver
            android:name="com.rokomari.new_package.notification_check.BootReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

完成所有这些操作后,我仍然遇到相同的问题。

----问题是-----

当我重新使用应用程序(打开应用程序)时,无论该应用程序是否在后台,都会收到来自一个信号的推送通知。但是当我一段时间不使用应用程序时,它就不会出现。此外,对于主要是Xiomi的某些设备,根本没有推送通知

在Xiomi设备中进行了如下所示的更改后-

***小米-确保在设置中为您的应用启用了“自动启动”属性。**

更改设置后,推送通知到来一次。但是我需要以编程方式解决方案,以发送推送通知以覆盖所有设备。如果使用One Signal还无法实现,那么我想知道Facebook,whatsapp等应用如何发送推送通知

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)