2017-04-19 82 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

相关问题