如何使用OneSignal向移动设备发送数据而不显示通知?

时间:2017-08-21 08:29:36

标签: onesignal

我想用OneSignal将json字符串发送到我的应用程序,但我不希望显示通知。

可以这样做吗?

2 个答案:

答案 0 :(得分:1)

在application.java中添加此行,其中onesignal用于初始化:

.setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())

所以你应该有这样的东西:

OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
            .unsubscribeWhenNotificationsAreDisabled(true)
            .init();

创建ExampleNotificationReceivedHandler类:

public class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
@Override
public void notificationReceived(OSNotification notification) {

    JSONObject data = notification.payload.additionalData;
    String datavalue;


    if (data != null) {
        datavalue = data.optString("datakey", null);
        //do what ever you want with value
     }

}

您应该使用onesignal新通知页面中的“其他数据”部分发送背景数据,并且不要忘记打开“背景数据”

答案 1 :(得分:0)

您需要的是无声通知。

  

无声通知是在设备上不显示任何消息的背景通知。

其中

  

后台通知旨在通过提供“唤醒”应用以在后台刷新数据的方式,使您的应用数据保持最新。

您需要Send a Silent Notification,然后在客户端覆盖onNotificationProcessing方法,您可以在其中检索数据并返回true以停止显示通知。

查看Background Data and Notification Overriding上的文档。