是否可以理解通知来自哪个客户?

时间:2014-07-13 20:50:41

标签: android parse-platform

我成功向一个客户发送通知。用这种方法:

ParsePush push = new ParsePush();
String yourMessage = "hello world";
push.setChannel("seconddevice");
push.setMessage(yourMessage);
push.sendInBackground();

我的申请:

public class ParseApplication extends Application {
    String YOUR_APPLICATION_ID="xxx",YOUR_CLIENT_KEY="yyy";

    @Override
    public void onCreate() {
        super.onCreate();

        // Add your initialization code here
        Parse.initialize(this, YOUR_APPLICATION_ID, YOUR_CLIENT_KEY);


        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();

        // If you would like all objects to be private by default, remove this line.
        defaultACL.setPublicReadAccess(true);

        ParseACL.setDefaultACL(defaultACL, true);

        PushService.subscribe(this, DEVICE_NAME, NotificationBck.class);


    }

}

有效。但是当我在第二个设备上收到消息时,我可以让哪个设备发送此通知吗?

1 个答案:

答案 0 :(得分:1)

来自Parse Android docs

  

传递给接收器的Intent对象包含一个带有两个有用映射的附加Bundle。 com.parse.Channel键指向表示发送消息的通道的字符串。 com.parse.Data键指向一个字符串,表示在推送通知中设置的“数据”字典的JSON编码值。

因此,在您的接收器中,您可以检查数据(假设您在推送时设置了它):

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    String channel = intent.getExtras().getString("com.parse.Channel");
    String encodedJson = intent.getExtras().getString("com.parse.Data");

    Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
    JSONObject json = decodeJsonObjectFrom(encodedJson);
    logContentsOf(json);
}

private JSONObject decodeJsonObjectFrom(String encodedJson) {
    try {
        return new JSONObject(encodedJson);
    } catch (JSONException e) {
        return new JSONObject();
    }
}

private void logContentsOf(JSONObject json) {
    while (json.keys().hasNext()) {
        String key = (String) json.keys().next();
        Log.d(TAG, "..." + key + " => " + getStringFrom(json, key));
    }
}

private String getStringFrom(JSONObject json, String key) {
    try {
        return (String) json.get(key);
    } catch (JSONException e) {
        return "";
    }
}