Hello,
I’d like to use notifications, but I’m not sure of what to do.
Here’s what will happen:
- a user A does something related to a user B, and the action is “stored” in the database
- the user B should ne notified
I thought I should use push notifications, but in this case, I don’t want to use an external site to send the notification: I need the notification to be sent when the user A does the action.
I was thinking of local notification, but I still have the problem: local notification will be triggered on the device of user A, but not from user A to user B on another device…
How do you think I should do ?
Thanks for your help.
That is a problem exclusively solved by push notifications.
However, if you want to do it with local notifications then you need some kind of polling functionality in your app to check the DB. If you want this to run in the background then you should install cordova-plugin-background-mode. I don’t advise this solution as its inefficient but it certainly is a workaround. I did not test this implementation.
.service('NotificationSrv', function($rootScope, $http, $interval, $cordovaLocalNotification) {
$ionicPlatform.ready(function () {
var stop;
var srv = {};
$rootScope.$on('$cordovaLocalNotification:trigger',
function (event, notification, state) {
// *** do something when the local notification triggers
});
// call this in your controller to start polling
srv.startPolling = function() {
// enable background mode so that the app is active when not in foreground
cordova.plugins.backgroundMode.enable();
stop = $interval(function() {
console.log('I am polling');
$http.get('http://your_data_base.com/api/check')
.then(processRes, processErr);
}, 60000); // runs every minute, value is in milliseconds
}
// call this in your controller to stop polling
srv.stopPolling = function() {
cordova.plugins.backgroundMode.disable();
if (angular.isDefined(stop)) {
$interval.cancel(stop);
stop = undefined;
}
}
function processRes(res) {
// database has appropriate value to fire local notification
if (res.check) {
$cordovaLocalNotification.schedule({
id: 1,
title: 'Local Notification',
text: '',
data: {
customProperty: 'custom value'
}
}).then(function (result) {
// ...
});
} else {
// do nothing
}
}
function processErr(err) {
console.error(JSON.stringify(err));
}
return srv;
})