node.js - 在 Ionic 中使用 ngCordova 和 PushPlugin 的 azure 通知中心客户端代码

标签 node.js azure push-notification ionic-framework cordova-plugins

哪个插件适合用于 Azure 通知中心、 ionic 应用程序? Phonegap/Cordova 中有许多用于推送通知的插件,其中一些已被弃用。 请提供一些引用链接,获取 ionic 客户端实现 Azure 通知中心的代码片段以及 Node.js 服务器端实现的代码片段。

最佳答案

在我的测试 Ionic 项目中,我使用了移动服务插件存储库 https://github.com/Azure/azure-mobile-services-cordova.git和 PushPlugin 位于 https://github.com/phonegap-build/PushPlugin.git .

以下是集成 GCM 的代码片段:

初始移动服务客户端和 GCM 配置:

    /****
    Mobile service configs
    ****/
    var mobileServiceClient;
    var mobileServiceUrl = 'https://<your_mobile_service_name>.azure-mobile.net/';
    var mobileSerivceKey = '<your service key>';

    /****
    Notification hubs configs
    *****/
    var GCM_SENDER_ID = 'gcm_sender_id_number'; // Replace with your own ID.
    var androidConfig = {
        "senderID": GCM_SENDER_ID,
      };

Angular 模块的应用配置中配置 GCM 和 Azure 通知中心:

angular.module('myapp', ['ionic','ngCordova'])
.run(function($ionicPlatform,$rootScope,$cordovaPush) {
  $ionicPlatform.ready(function() {

    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    //init mobileServiceClient
    mobileServiceClient = new WindowsAzure.MobileServiceClient(mobileServiceUrl,mobileSerivceKey);
    //register notification push
    console.log("======mobileServiceClient inited going on ======");
    $cordovaPush.register(androidConfig).then(function(result) {
      // Success
      console.log("=============register finished========");
    }, function(err) {
      // Error
    })
    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
      switch(notification.event) {
        case 'registered':
          if (notification.regid.length > 0 ) {
            $rootScope.GCM_status = true;
            alert('registration ID = ' + notification.regid);
              //register to Azure Mobile Service
            if (mobileServiceClient) {
              // Template registration.
              var template = '{ "data" : {"message":"$(message)"}}';
              // Register for notifications.
              mobileServiceClient.push.gcm.registerTemplate(notification.regid,
                "myTemplate", template, null)
              .done(function () {
                  $rootScope.Azure_Push_status = true;
                  alert('Registered template with Azure!');
              });
              // .fail(function (error) {
              //     alert('Failed registering with Azure: ' + error);
              // });
            }
          }
          break;

        case 'message':
          // this is the actual push notification. its format depends on the data model from the push server
          alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
          break;

        case 'error':
          alert('GCM error = ' + notification.msg);
          break;

        default:
          alert('An unknown GCM event has occurred');
          break;
      }
    });
    // WARNING: dangerous to unregister (results in loss of tokenID)
    $cordovaPush.unregister().then(function(result) {
      // Success!
    }, function(err) {
      // Error
    })

  });
})

请引用Microsoft Azure : Push Notifications to Cordova Apps with Microsoft Azure有关在 Cordova 应用程序中集成 Azure 通知中心的更多信息。

关于node.js - 在 Ionic 中使用 ngCordova 和 PushPlugin 的 azure 通知中心客户端代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33470834/

相关文章:

node.js - user.save() 不会在 mongodb 中保存(不执行任何操作)

c# - 如何在 TableOperation 中从 Azure 表存储中检索实体列表而不指定 rowKey?

Azure 网站部署 - 找不到用户 - 扩展代码 09004

iOS 推送通知没有声音

android - 在 android 上使用 phonegap 设置 Urban Airship 推送通知

node.js - 针对经过身份验证的用户的 Express 静态

Node.Js 流管道上的异步迭代器

javascript - 是否可以将渲染函数放入 Express 的 for 循环中?

azure - 在 Azure Pipeline 任务中循环多个参数

iphone - 需要帮助将带有设备 token 的 URL 从我们的 iPhone 应用程序发送到我们的数据库