2017-04-19 80 views
0

如何在初始註冊後註冊其他推送通知標籤?下面的代碼註冊成功標籤(arrTags):科爾多瓦推送通知 - 註冊標籤

pushRegistration.on('registration', function (data) { 
    ... 
    if (platform == 'android' || platform == 'Android') { 
     // Register for GCM notifications. 
     AzureDbSvc.client.push.register('gcm', handle, { 
     mytemplate: { body: { data: { message: "{$(messageParam)}" } }, tags: arrTags } 
     }); 
    } 
    ... 
} 

現在的標籤註冊,我如何註冊更多的標籤?例如,如果arrTags最初包含4個標籤,我將如何(隨後)註冊第5或第6個標籤?

回答

0

下面是我的代碼 - 添加新的標籤工作沒有重新初始化。請讓我知道任何建議。

用法:

  1. 初始化 - 呼叫registerForPushNotifications(arrTags)。
  2. 添加或刪除標籤 - 使用全套標籤 (少於任何要刪除的)調用registerTags(arrTags)。
  3. 註銷所有標籤 - 電話註銷()

代碼:

appServices.factory('AzurePshNtfnSvc', function ($ionicPopup, MsgSvc) { 
var pushRegistration = null; 
var regData = null; 
... 

/// Push Notification Registration /// 
function registerForPushNotifications(arrTags) { 
    pushRegistration = PushNotification.init({ 
     android: { senderID: 'YourID#' }, 
     ios: { alert: 'true', badge: 'true', sound: 'true' }, 
     wns: {} 
    }); 

    // Handle the registration event. 
    pushRegistration.on('registration', function (data) { 
     regData = data; 
     registerTags(arrTags); 
    }); 

    pushRegistration.on('notification', function (data) { 
     alert('Push Received: ' + data.message); 
     MsgSvc.prepForPushNotification(data); 
    }); 

    pushRegistration.on('error', handleError); 
} 

// Now I can call AzurePshNtfnSvc.registerTags from anywhere in the app 
// and delete or add a tag. 
function registerTags(arrTags) { 
    // Get the native platform of the device. 
    var platform = device.platform; 
    // Get the handle returned during registration. 
    var handle = regData.registrationId; 
    // Set the device-specific message template. 
    if (platform == 'android' || platform == 'Android') { 
     // Register for GCM notifications. 
     AzureDbSvc.client.push.register('gcm', handle, { 
      mytemplate: { body: { data: { message: "{$(messageParam)}" } }, tags: arrTags } 
      // example: mytemplate: { body: { data: { message: "{$(messageParam)}" } }, 
      //   tags: ["mynotificationtag", "anothertag"]}  
      // site: https://github.com/Azure/azure-mobile-apps-cordova-client/issues/32 
     }); 
    } else if (device.platform === 'iOS') { 
     // Register for notifications. 
     AzureDbSvc.client.push.register('apns', handle, { 
      mytemplate: { body: { aps: { alert: "{$(messageParam)}" } } } 
     }); 
    } else if (device.platform === 'windows') { 
     // Register for WNS notifications. 
     AzureDbSvc.client.push.register('wns', handle, { 
      myTemplate: { 
       body: '<toast><visual><binding template="ToastText01"><text id="1">$(messageParam)</text></binding></visual></toast>', 
       headers: { 'X-WNS-Type': 'wns/toast' } 
      } 
     }); 
    } 
} 

// Unregister all tags, called when exiting app 
function unregister() { 
    return new Promise(function (resolve, reject) { 
     if (pushRegistration == null) { 
      return resolve(); 
     } else { 
      pushRegistration.unregister(function() { 
       console.log('success'); 
       resolve(); 
      }, function() { 
       console.log('error'); 
       reject(); 
      }); 
     } 
    }); 
} 
... 
1

您可以通過調用此功能AzureDbSvc.client.push.register()更新註冊與標籤,因爲註冊本身是暫時的。

此外,您可以嘗試經理後端註冊,你可以參考Registration Management

+0

感謝加里!這工作。問題,我是否需要重新註冊所有標籤或僅附加一個標籤?請參閱我的解決方案和建議的改進。 – Mike

+0

通過測試,我發現所有標籤即使追加一個標籤也需要重新註冊。看我的解決方案。 – Mike

相關問題