OSNotificationPayload 无法转换为 JSONObject

OSNotificationPayload cannot be converted to JSONObject

情况:

在我的 Quasar hybrid app I need to implement some native functionality to receive background notifications.

我使用 OneSignal 从我的 API 发送推送通知。

在有效载荷中,我添加了一个 notification_type 来判断通知是否静音(是否必须显示在 phone 中)。

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

代码:

这是通知服务:

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;

参考文献:

以下是 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 对象吗?

你知道我为什么会收到这个错误吗?

您拥有的负载是 OSNotificationPayload 类型而不是 JSONObject 因此您需要像这样阅读它:

OSNotificationPayload object = receivedResult.payload;

然后你从这个对象中读取值。

试试这个代码示例

        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:"";

@Khalid Taha 的回答是正确的。

但是对于我的具体情况,我可以使用 JSONObject。问题出在我的使用方式上。

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

JSONObject additionalData = receivedResult.payload.additionalData;

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

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

可能 属性 additionalData 是通知的内置 属性。