3

使用解析使用集解析DeviceToken爲Android

ParseInstallation installation =ParseInstallation.getCurrentInstallation(); 
     installation.put("GCMSenderId",senderId); 
     installation.put("pushType","gcm"); 
     installation.put("deviceToken",token); 

IM我不能設置在安裝表中的設備標識,但是當我嘗試使用保存我得到一個例外。

Cannot modify `deviceToken` property of an _Installation object. android 

的問題是,後端使用這種tokenId發送推送通知的另一個提供商(OneSignal),所以我想知道是否它的任何方式對deviceToken行寫(據我所知,這是僅限iOS)。 我需要在deviceToke中寫入我收到的GCM令牌。 謝謝

+0

如果您希望遷移到OneSignal,只需添加'GCMSenderId',因爲OneSignal解析導入器在您進行此更改後將能夠引入您的現有用戶。 https://onesignal.com/blog/important-note-for-android-parse-push-users/ – jkasten

+0

如果sdk不允許你,你可以嘗試使用currentUser標記來休息api。 – ChunTingLin

+0

@jkasten它能夠把我的用戶,但沒有更新更新deviceToken,我無法推動成功發送。 – jaynp

回答

1

我通過添加稱爲setDeviceToken的解析雲功能解決此問題。這樣我可以使用MasterKey修改安裝記錄。

Parse.Cloud.define("setDeviceToken", function(request, response) { 
    var installationId = request.params.installationId; 
    var deviceToken = request.params.deviceToken; 

    var query = new Parse.Query(Parse.Installation); 
    query.get(installationId, {useMasterKey: true}).then(function(installation) { 
     console.log(installation); 
     installation.set("deviceToken", deviceToken); 
     installation.save(null, {useMasterKey: true}).then(function() { 
      response.success(true); 
     }, function(error) { 
      response.error(error); 
     }) 
    }, function (error) { 
     console.log(error); 
    }) 
}); 

然後,在我的Android應用程序的Application.onCreate

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() { 
    @Override 
    public void idsAvailable(String userId, String registrationId) { 
     final ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation(); 

     if (registrationId != null) { 
      HashMap<String, Object> params = new HashMap<>(2); 
      params.put("installationId", currentInstallation.getObjectId()); 
      params.put("deviceToken", registrationId); 

      ParseCloud.callFunctionInBackground("setDeviceToken", params, new FunctionCallback<Boolean>() { 
       @Override 
       public void done(java.lang.Boolean success, ParseException e) { 
        if (e != null) { 
         // logException(e); 
        } 
       } 
      }); 
     } 
    } 
}); 
+1

這是一個有用的解決方法,我確認正在使用ParseServer v2.3.5和Android SDK v1.14.1。如果您使用的是Firebase'String registrationId = FirebaseInstanceId.getInstance()。getToken();'只是好奇,爲什麼選擇onebaseal over Firebase @jaynp? –

+0

@JaimeAgudo OneSignal輕鬆支持A/B測試,iOS 10媒體附件,動作按鈕等一系列高級功能。我現在沒有全部使用它們,但很高興有這個選擇! – jaynp

0

對於未來的遊客,我解決了這個以不同的方式,通過解析REST API:http://parseplatform.org/docs/rest/guide/

利用這一點,你可以繞過常規的SDK限制。只需向https://yourApp/parse/installtions/ {installationObjectIDHere}發出放入請求即可。創建一個字典並將其編碼爲JSON:

Map<String, String> dictionary = new HashMap<String, String>(); 
    dictionary.put("deviceToken", yourToken); 
    JSONObject jsonDictionary = new JSONObject(dictionary); 
    String bodyAsString = jsonDictionary.toString(); 
    RequestBody body = RequestBody.create(JSON, bodyAsString); 

然後發送請求,並且應該工作。