我正在尝试将推送通知集成到我的Ionic / Cordova应用程序中。我正在使用OneSignal服务进行集成。我已成功设置iPhone以接收来自OneSignal网络界面的推送通知。
下一步是获取pushToken(OneSignal / Apple用来发送推送 通知单个设备)填充我的应用程序的离子 控制器,以便我可以实现应用程序逻辑和推送消息 基于应用事件。
我安装了他们的(OneSignal)Cordova插件和我的iPhone注册,它报告了pushToken。但是我无法将pushToken字符串传递给我的任何控制器。我怎样才能做到这一点?下面是我的app.js和controller.js。
app.js
angular.module('cApp', ['ionic', 'cApp.controllers'])
.run(function($ionicPlatform, $cordovaSplashscreen, $rootScope) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true); // Fixes keyboard issue
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
/*===========================================================================
http://documentation.onesignal.com/v2.0/docs/phonegap--cordova-sdk-api#init
===========================================================================*/
window.plugins.OneSignal.init("5eb87dse-b458-11e3-ac11-000c2940e62c",
{googleProjectNumber: "",
autoRegister: true},
app.didReceiveRemoteNotificationCallBack);
window.plugins.OneSignal.getIds(function(ids) {
console.log('getIds: ' + JSON.stringify(ids)); // I can see PushToken and UserId in the console.
$rootScope.pushToken = ids.pushToken;
});
});
console.log($rootScope.pushToken);
})
// Configure Routes....... etc.
controller.js
.controller('MenuCtrl', function($scope, $rootScope, $ionicModal, $ionicPlatform) {
console.log($rootScope.pushToken); // It comes back undefined :(
答案 0 :(得分:1)
听起来app.js在你的controller.js之前运行。如果是这种情况,您应该能够执行以下操作;
以下是实现这两个流程的代码:
<强> app.js:强>
didReceiveRemoteNotificationCallBack(message, additionalData, isActive) {
var notificationObj = {message: message, additionalData: additionalData, isActive: isActive};
if ($rootScope.notificationReceivedHandler)
$rootScope.notificationReceivedHandler(notificationObj);
else
$rootScope.openedFromNotification = notificationObj;
}
<强> controller.js:强>
.controller('MenuCtrl', function($scope, $rootScope, $ionicModal, $ionicPlatform) {
if ($rootScope.openedFromNotification)
processNotificationOpened($rootScope.openedFromNotification);
$rootScope.openedFromNotification = processNotificationOpened;
}
function processNotificationOpened(notificationObj) {
// Read and process what you need to here.
}
注意:上述代码未经过测试。