Sky*_*sey 5 android parse-platform
我想将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.
}
}
});
Run Code Online (Sandbox Code Playgroud)
在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.
}
});
Run Code Online (Sandbox Code Playgroud)
通过继承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);
}
Run Code Online (Sandbox Code Playgroud)
该字段的用途deviceToken是存储通过 GCM 将消息传送到该设备所需的订阅 ID。拦截 GCM 注册调用以获取注册 ID,其唯一目的是将推送通知定向到该设备可能不是最好的方法。
虽然您可以在查询中使用 来deviceToken将推送通知定位到此设备,但您可能需要考虑使用安装对象本身的任何其他字段。请记住,ParseInstallation对象扩展了ParseObject,因此您可以向安装对象添加任何字段,以帮助您将推送通知定位到该设备。
如果您没有任何具体标准来帮助您唯一识别应用程序的这一用户,并且您正在寻找唯一标识符,我是否可以建议您将其保存objectId在您的服务器上以用于定位目的?objectId无论 GCM 注册过程的当前状态如何,只要执行安装保存回调即可使用。
| 归档时间: |
|
| 查看次数: |
410 次 |
| 最近记录: |