首页 > 解决方案 > 如何解决 - 如果应用程序未打开一段时间,Onesignal 推送通知不会发送到 Android 设备?

问题描述

在我的 Android 应用程序中,我使用One Signal 推送通知服务来发送推送通知。我已经根据文档完成了所有设置。

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

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>

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

我使我的应用程序成为系统应用程序并添加了一个身份验证服务。我还添加了一个广播接收器类,如下所示 -

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>

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

- - 问题是 - - -

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

在小米设备中进行如下更改后-

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

更改设置后,推送通知来了一次。但我需要一个以编程方式发送推送通知以覆盖所有设备的解决方案。如果 One Signal 尚无法实现,那么我想知道 facebook、whatsapp 等应用程序如何发送推送通知?

标签: javaandroidpush-notificationfirebase-cloud-messagingonesignal

解决方案


推荐阅读