在控制器中调用 FCM 工厂以获取通知数据
Call FCM factory in a controller to get notification data
自上周以来,我一直在尝试使用 Phonegap 和 AngularJS 从 FCM 获取通知。
我可以用 cordova-fcm-plugin 做到这一点,所以现在我想从消息中获取数据,他们建议使用此代码:
FCMPlugin.onNotification(
function(data){
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert( JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert( JSON.stringify(data) );
}
},
function(msg){
console.log('onNotification callback successfully registered: ' + msg);
},
function(err){
console.log('Error registering onNotification callback: ' + err);
}
);
我的问题是我不知道如何将该代码添加到 angular 控制器,所以我在互联网上搜索了类似的东西,我发现了这个 factory
angular.module('rhoAppApp')
.factory('$FCMPlugin', $FCMPlugin);
$FCMPlugin.$inject = [];
function $FCMPlugin() {
var service = {
getToken: function(successCallback, failCallback) {
FCMPlugin.getToken(successCallback, failCallback);
},
onNotification: function(onNotification, onCallbackSuccesSet, onCallbackFailSet) {
FCMPlugin.onNotification(onNotification,
onCallbackSuccesSet, onCallbackFailSet);
}
};
return service;
}
所以现在我的问题是在我的控制器中使用那个工厂,我知道(也许我错了)你必须从以下位置调用它:
.controller('MainCtrl', function ($FCMPlugin) {
$FCMPlugin.something
})
但是我不知道如何使用那个工厂,我以前从来没有用过。
我可以用这个解决这个问题:
我使用 phonegap + yeoman 进行了构建 angular,使用此方法构建的问题之一是您必须包含 cordova.js
我的问题是,我在这一行中包含 condorva.js
<!-- build:js(.) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<!-- endbower -->
<!-- endbuild -->
这是一个问题,因为您不想在 vendor.js 中构建它,而是我在该行之外添加了 cordova.js:
然后我将此代码添加到 app.js 以识别您何时在移动设备或网络中:
angular.element(document).ready(function () {
if (navigator.userAgent.match(/(iOS|iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener('deviceready', function () {
console.log('This is mobile app');
angular.bootstrap(document.body, ['moodleAppApp']);
}, false);
} else {
console.log('This is web app');
angular.bootstrap(document.body, ['moodleAppApp']);
}
});
我还从 index.html
中删除了 ng-app
所有这些使我的 angular 应用程序可以与 cordova 一起工作。
自上周以来,我一直在尝试使用 Phonegap 和 AngularJS 从 FCM 获取通知。
我可以用 cordova-fcm-plugin 做到这一点,所以现在我想从消息中获取数据,他们建议使用此代码:
FCMPlugin.onNotification(
function(data){
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert( JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert( JSON.stringify(data) );
}
},
function(msg){
console.log('onNotification callback successfully registered: ' + msg);
},
function(err){
console.log('Error registering onNotification callback: ' + err);
}
);
我的问题是我不知道如何将该代码添加到 angular 控制器,所以我在互联网上搜索了类似的东西,我发现了这个 factory
angular.module('rhoAppApp')
.factory('$FCMPlugin', $FCMPlugin);
$FCMPlugin.$inject = [];
function $FCMPlugin() {
var service = {
getToken: function(successCallback, failCallback) {
FCMPlugin.getToken(successCallback, failCallback);
},
onNotification: function(onNotification, onCallbackSuccesSet, onCallbackFailSet) {
FCMPlugin.onNotification(onNotification,
onCallbackSuccesSet, onCallbackFailSet);
}
};
return service;
}
所以现在我的问题是在我的控制器中使用那个工厂,我知道(也许我错了)你必须从以下位置调用它:
.controller('MainCtrl', function ($FCMPlugin) {
$FCMPlugin.something
})
但是我不知道如何使用那个工厂,我以前从来没有用过。
我可以用这个解决这个问题:
我使用 phonegap + yeoman 进行了构建 angular,使用此方法构建的问题之一是您必须包含 cordova.js
我的问题是,我在这一行中包含 condorva.js
<!-- build:js(.) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<!-- endbower -->
<!-- endbuild -->
这是一个问题,因为您不想在 vendor.js 中构建它,而是我在该行之外添加了 cordova.js:
然后我将此代码添加到 app.js 以识别您何时在移动设备或网络中:
angular.element(document).ready(function () {
if (navigator.userAgent.match(/(iOS|iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener('deviceready', function () {
console.log('This is mobile app');
angular.bootstrap(document.body, ['moodleAppApp']);
}, false);
} else {
console.log('This is web app');
angular.bootstrap(document.body, ['moodleAppApp']);
}
});
我还从 index.html
中删除了 ng-app所有这些使我的 angular 应用程序可以与 cordova 一起工作。