我想将GCM deviceToken
发送到我的服务器,以便我可以使用Parse的REST API启动推送通知。这一切都有效,只是当它变得可用时我无法可靠地获得deviceToken
。当我注册应用程序以在广播频道上接收推送通知时,我会检查deviceToken
回调中的done()
。但是,它通常尚未设定。我正在寻找一种方法来获取deviceToken
它可用的那一刻,所以我可以避免轮询或等待应用程序重新启动以发送推送通知。
在频道注册回调中抓取deviceToken
Parse.initialize(this, applicationId, clientKey) {
ParsePush.subscribeInBackground("", new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
String deviceToken = (String) ParseInstallation.getCurrentInstallation().get("deviceToken");
// deviceToken is often still null here.
}
}
});
在ParseInstallation.saveInBackground()中抓取deviceToken
final ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
parseInstallation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
String deviceToken = (String) parseInstallation.get("deviceToken");
// deviceToken is often still null here.
}
});
通过继承com.parse.GcmBroadcastReceiver
来自己监听GCM注册事件// Which I can't do, because it's declared final.
public final void onReceive(Context context, Intent intent) {
PushService.runGcmIntentInService(context, intent);
}
答案 0 :(得分:2)
deviceToken
字段的用途是存储通过GCM将消息传递到此设备所需的订阅ID。拦截GCM注册呼叫以获取注册ID仅用于将推送通知定向到此设备,这可能不是最好的方法。
虽然您可以在查询中使用deviceToken
来定位此设备的推送通知,但您可能需要考虑使用安装对象本身的任何其他字段。请记住,ParseInstallation
对象扩展ParseObject
,因此您可以向安装对象添加任何字段,以帮助您将推送通知定位到此设备。
如果您没有任何特定条件可帮助您识别应用中的这一位用户并且您正在寻找唯一标识符,我建议您在服务器上保存objectId
用于定位目的?无论GCM注册过程的当前状态如何,只要执行安装保存回调,objectId
就可用。