在 android 中关闭应用程序时,在真实设备上未收到一个信号通知

One Signal Notification did not receive on real device while application is closed in android

我已经为 One Signal Push 通知创建了演示应用程序。它在模拟器上工作正常,但在真实设备上测试时。应用关闭没有收到推送通知时出现1个问题

实现代码如下:

TestDemo.java 文件

public class TestDemo extends Application {

@Override
public void onCreate() {
    super.onCreate();
    OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.WARN);

    OneSignal.startInit(this)
            .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
            .autoPromptLocation(true)
            .init();
}

private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;
        String customKey;

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

        if (actionType == OSNotificationAction.ActionType.ActionTaken)
            Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
    }
}
}

MainActivity.java 文件

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
        @Override
        public void idsAvailable(String userId, String registrationId) {
            Log.d("UserId : ", userId);
            Log.d("Reg Id : ", registrationId);
        }
    });

}

并且还作为文档建议获得许可

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

如果有解决办法请帮忙...

谢谢...

以下是通知可能在 OneSignal 控制面板或 API 上显示为已发送但在您的设备或网站上不可见的原因:

该应用目前处于焦点

默认情况下,如果您的应用当前 "in focus"(打开且可见),则不会在设备上显示通知。但是,您可以调用 inFocusDisplayingInAppAlert 以在您的应用程序中将通知显示为警告框,或 Notification 以显示通知。

应用程序被强制停止

当应用程序处于 "Force Stopped" 状态时,将不会收到大多数事件,包括 GCM/FCM 推送通知消息。可以通过以下方式将应用置于此状态。

  • 在“设置”>“应用”中,按下 "Force Stop"。
  • 在某些设备上长按后退按钮。
  • 使用像 Greenify 这样的第 3 方任务杀手。
  • 由于自定义 Android 调整,某些华为、小米或索尼设备上的应用已关闭。可以更改以下设备设置以防止出现这种情况。

    * Huawei - Go to Settings > "Protected apps", check your app.
    * Xiaomi  - Make sure "Auto-start" property enabled for your app in the settings.
    * Sony - Tap on the battery icon. Go to Power Management > STAMINA mode > Apps active in standby > Add your app.
    

要确认您的应用状态是否存在问题,请发送一些通知并检查以下 GCM logcat 条目。

W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.onesignal.example (has extras) }

一些设备制造商会将进入强制关闭状态的应用列入白名单。例如 Gmail 和 Whatsapp。

你有网络问题

您连接的网络/WiFi 可能已关闭您与 Google 服务器的连接。尝试禁用并重新启用您的互联网连接。有关详细信息,请参阅