首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cordova推送通知-注册标签

Cordova推送通知-注册标签
EN

Stack Overflow用户
提问于 2017-04-19 01:05:40
回答 2查看 543关注 0票数 0

如何在初始注册之后注册其他推送通知标记?以下代码成功注册标记(arrTags):

代码语言:javascript
复制
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个标记?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-21 15:01:28

下面是我的代码--添加新的标记,不需要重新初始化。请告诉我任何建议。

用法:

  1. 初始化-调用registerForPushNotifications(arrTags)。
  2. 添加或删除标记-调用标记(registerTags,arrTags),包含完整的标记数组(除任何要删除的标记外)。
  3. 注销所有标签-调用注销()

代码:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-04-19 09:28:59

您可以通过回忆这个函数AzureDbSvc.client.push.register()来更新带有标记的注册,因为注册本身是短暂的。

此外,您可以尝试在后端管理注册,您可以参考注册管理

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43484580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档