java - 如何解决 - 如果应用程序未打开一段时间,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 等应用程序如何发送推送通知?
解决方案
推荐阅读
- javascript - 未捕获的 ReferenceError:sakla 未在 HTMLImageElement.onclick 中定义
- c++ - GLM:如何创建一个 9 列和 3 行的矩阵
- angular - 如何在ionic中注册cordova事件
- vue.js - vue点击后如何重新加载值
- python - 如何在 sqlalchemy 中实现右连接?
- javascript - 我想将下面的 for 循环转换为 array.map() 但不知道如何
- ios - 从第三类内部将委托分配给另一个类的类
- ios - Delphi Firemonkey iOS AppStore 使用 ssl 静态库上传
- node.js - npm 安装后找不到 Docker 镜像错误 bin
- c# - 在运行时更改 DataTemplate