Last active
May 23, 2019 15:37
-
-
Save sean-hill/00aed2ad0c515d866f8f to your computer and use it in GitHub Desktop.
Pushwoosh Service for Ionic Apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ionic and Pushwoosh example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.controller('PushWooshCtrl', function($scope, $ionicPlatform, PushWoosh){ | |
// Initialize PushWoosh Plugin | |
PushWoosh.init('<PUSH_WOOSH_APP_ID>', '<GOOGLE_PROJECT_NUMBER>').then( | |
function(){ | |
// Register Device to get device token | |
PushWoosh.registerDevice().then( | |
function(deviceToken){ | |
// Device Token: | |
// a8458b8bf6eff472c6bf24ef5e2qf4fac0f30ef554fa9050e89d32357d2c976 | |
}, | |
function(err){ | |
// PushWoosh error | |
}) | |
; | |
}, | |
function(err) { | |
console.log('PushWoosh init error:', err); | |
} | |
); | |
// Wait for platform to be ready | |
$ionicPlatform.ready(function(){ | |
$scope.initPushWoosh(); | |
}); | |
// Listen for push notification events | |
$scope.$on('event:push-notification', function(event, notification) { | |
// Notification: | |
// { | |
// title: 'Your title' | |
// , meta: { | |
// someKey: 'youSpecified' | |
// } | |
// } | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.factory('PushWoosh', function ($q, $rootScope, $window) { | |
$window.addEventListener('push-notification', function(event){ | |
var notification = {}; | |
if (ionic.Platform.isIOS()) { | |
notification.title = event.notification.aps.alert; | |
notification.meta = event.notification.u; | |
} | |
if (ionic.Platform.isAndroid()) { | |
notification.title = event.notification.title; | |
notification.meta = event.notification.userdata; | |
} | |
// Not tested | |
if (ionic.Platform.isWindowsPhone()) { | |
notification.title = event.notification.content; | |
notification.meta = event.notification.userdata; | |
} | |
$rootScope.$broadcast('event:push-notification', notification); | |
}); | |
return { | |
init: function(pwAppId, googleProjectNumber) { | |
var defer = $q.defer(); | |
if (!pwAppId) { | |
defer.reject('Please provide your pushwoosh app id.'); | |
} | |
else if (!window.plugins || !window.plugins.pushNotification) { | |
defer.reject('Push Notification not enabled.'); | |
} | |
else { | |
var pushNotification = window.plugins.pushNotification; | |
if (ionic.Platform.isIOS()) { | |
pushNotification.onDeviceReady({ pw_appid: pwAppId }); | |
} | |
if (ionic.Platform.isAndroid()) { | |
pushNotification.onDeviceReady({ projectid: googleProjectNumber, appid : pwAppId }); | |
} | |
// Not tested | |
if (ionic.Platform.isWindowsPhone()) { | |
pushNotification.onDeviceReady({ appid: pwAppId, serviceName: '' }); | |
} | |
defer.resolve(); | |
} | |
return defer.promise; | |
}, | |
registerDevice: function() { | |
var defer = $q.defer(); | |
var pushNotification = window.plugins.pushNotification; | |
pushNotification.registerDevice( | |
function(status) { | |
var deviceToken; | |
if (ionic.Platform.isIOS()) { | |
deviceToken = status.deviceToken; | |
} | |
if (ionic.Platform.isAndroid()) { | |
deviceToken = status; | |
} | |
// Not tested | |
if (ionic.Platform.isWindowsPhone()) { | |
deviceToken = status; | |
} | |
defer.resolve(deviceToken); | |
}, | |
function(status) { | |
defer.reject(status); | |
} | |
); | |
return defer.promise; | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I tried this but I get this error "PushWoosh init error: Push Notification not enabled." thanks!