OSNotificationPayload无法转换为JSONObject

时间:2019-01-16 13:50:03

标签: java android cordova onesignal quasar-framework

情况:

在我的Quasar混合应用程序中,我需要实现一些本机功能才能接收background notifications

我通过OneSignal从我的API发送推送通知。

在有效负载中,我添加了一个notification_type,它将告知通知是否静音(必须在电话中显示)。

当我收到通知时,我需要读取该有效负载,但是我却无法管理。

代码:

这是NotificationService:

package com.myapp.app;

import android.util.Log;
import org.json.JSONObject;

import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationReceivedResult;

public class NotificationService extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {

     if (receivedResult != null) {

        JSONObject data = receivedResult.payload;
        // check data - if notification_type is 'silent' than return true otherwise return false
        return false;
     }
   }
}

错误:

error: incompatible types: OSNotificationPayload cannot be converted to JSONObject
        JSONObject data = receivedResult.payload;

enter image description here

参考:

以下是OneSignal Android SDK存储库中的一些示例:

https://github.com/OneSignal/OneSignal-Android-SDK/blob/master/Examples/AndroidStudio/app/src/main/java/com/onesignal/example/NotificationExtenderExample.java

它涉及后台通知,但在这种情况下,它们不会读取receivedResult的内容。

这是我关注的一个很好的例子:

https://www.programcreek.com/java-api-examples/?code=AppHero2/Raffler-Android/Raffler-Android-master/app/src/main/java/com/raffler/app/service/NotificationService.java#

在这种情况下,它将读取以下数据: JSONObject additionalData = receivedResult.payload.additionalData;

API:

这是我从Laravel API发送推送通知的方式

private function send_notification_curl($order) {
    $content      = array(
        "en" => "notification message...",
    );
    $fields = array(
        'data' => array(
            'order_id' => $order->id,
            'notification_type' => 'silent'
        ),
        'contents' => $content,
        // some other params...
    );

    $fields = json_encode($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic my_key'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

问题:

我如何阅读receivedResult的内容?

我可以将其转换为json对象吗?

您知道我为什么会收到该错误吗?

3 个答案:

答案 0 :(得分:2)

您拥有的有效载荷属于OSNotificationPayload类型,而不是JSONObject,因此您需要像这样读取它:

OSNotificationPayload object = receivedResult.payload;

然后您从该对象读取值。

答案 1 :(得分:0)

尝试此代码示例

        JSONObject data = result.notification.payload.additionalData;
        String message=result.notification.payload.body!=null?result.notification.payload.body:"";
        String title=result.notification.payload.title!=null?result.notification.payload.title:"";

答案 2 :(得分:0)

@Khalid Taha的回答是正确的。

但是对于我的特定情况,我可以使用JSONObject。出问题的是我如何使用它。

如果我以这种方式访问​​数据,它将起作用:

JSONObject additionalData = receivedResult.payload.additionalData;

然后我可以得到像这样的单个参数:

final String notificationType = additionalData.optString("notification_type");

属性additionalData可能是Notifications的内置属性。