1. 程式人生 > >關於ionic3 極光推送 Registration ID第一次獲取不到

關於ionic3 極光推送 Registration ID第一次獲取不到

問題:

  1. 關於Registration ID第一次獲取不到,可以用setTimeout延遲後獲取。
  2. ios開發環境能收到通知訊息,生產模式接收不到通知訊息。
  3. ios釋出到app store上,下下來的app仍然獲取不到id。使用xcode連線iPhone安裝的app,就能獲取到id。

版本:

  • lonic3
  • angular cli:6.0.7
  • node:8.11.1

關於如何下載jpush外掛及極光推送文件:

具體程式碼:

  • 業務需求:登入時獲取到Registration ID後關聯此賬戶,然後把ID傳送給後臺。
  • 首先 初始化jpush
    • app.component.ts
 platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
      /* 驗證是否在 app 中 */
      if (platform.is('cordova')) {
        /* 更新APP */
        this.hotCode.hotCodeUpdate();
        /* 極光推送 */
        jpush.init();
        jpush.setDebugMode(false);
      }
    })
;
  • 在登入介面獲取到token後傳送id
    • login.ts
public doLogin = () => {

      this.ajax.loadData({
        uri: 'login',
        method: 'post',
        title: '登入請求',
        data: this.loginForm.value,
        //afterFn: afterFn
      }).subscribe(res => {
        this.msg.hideLoader();
        if (res.status === 1
) { const crypt_token: string = res.payload.crypt_token; const zhiwei: string = res.payload.zhiwei; this.storage.set('zhiwei', zhiwei); this.storage.set('crypt_token', this.stringCode.encode(crypt_token)).then(() => { this.events.publish('login', res); this.viewCtrl.dismiss(); //獲取token後,獲取極光推送id,然後傳送id setTimeout(() => { this.jpush.getRegistrationID().then(res => { this.registrationID = res; // alert('login時獲取id:'+this.registrationID); this.getJiGuangID(this.registrationID); }); }, 1000); }); } }); } } /** * @description 傳送極光id * @private * @memberof LoginPage */ public getJiGuangID = (id) => { this.ajax.loadData({ uri: `jiguang_info/` + id, method: 'get', title: '傳送極光推送 registrationID ' }).subscribe(res => { // alert('login時傳送id成功。') }) }

問題解決:

  • ios開發環境能收到通知訊息,生產模式接收不到通知訊息。

解決方案

  • xcode上找到 TARGET -> Capabilities -> Push Notification 選項點開
  • xcode上找到 TARGET -> Build Setting -> Code Signing Identity -> Code Signing Entitlements *****Entitle-release.plist 看看有沒有 aps-environment 欄位,沒有補上(關鍵是這一步,加上欄位就成功了!)
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>

注意點:

  • 取不到 token 就取不到 registrationID。(這個坑了我好久。。。)
  • 關於Registration ID第一次獲取不到,可以用setTimeout延遲後獲取
  • Registration ID還是獲取不到,請檢查證書及各個id是否對應。

參考文件: