我使用以下云代码向Android设备发送解析通知:
Parse.Cloud.define('SendNot', function(request, response)
{
var query = new Parse.Query(Parse.Installation);
query.equalTo('channels', 'Customer');
Parse.Push.send
({
where: query ,
data:
{
alert: "Hai customers",
flag: "true"
}
},
{
useMasterKey : true,
success: function()
{
response.success("true");
},
error: function(error)
{
response.error("error");
}
});
});
我想将数据存储在“flag”字段中。我使用以下代码接收通知:
@Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Received Notification");
jsonString = intent.getExtras().getString("com.parse.Data");
try {
json = new JSONObject(jsonString);
System.out.println(json.getString("flag") + " Notification content");
} catch (Exception e) {
e.printStackTrace();
}
}
我能够收到通知。但是,我无法获得“旗帜”的价值。它显示以下异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.length()' on a null object reference
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at com.hashcoder.eegoomain.MyBroadcastReceiver.onReceive(MyBroadcastReceiver.java:31)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at android.app.ActivityThread.access$1800(ActivityThread.java:153)
03-11 10:56:48.170 22214-22214/com.hashcoder.eegoomain W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1428)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at android.os.Looper.loop(Looper.java:148)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5441)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
03-11 10:56:48.171 22214-22214/com.hashcoder.eegoomain W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
代码有什么问题?
答案 0 :(得分:2)
创建另一个扩展ParsePushBroadcastReceiver并在该类上调用onPushReceive函数的类。
public class ClassName extends ParsePushBroadcastReceiver
{
String jsonString;
JSONObject json;`
protected void onPushReceive(Context mContext, Intent mintent)
{
jsonString = mintent.getExtras().getString("com.parse.Data");
try {
json = new JSONObject(jsonString);
System.out.println(json.getString("flag") + " Notification content");
} catch (Exception e) {
e.printStackTrace();
}
}
}
将此类作为接收器添加到清单文件中:
<receiver
android:name=".ClassName"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>