I’ve hit a small snag with Ionic Push—even though pluginConfig specifies badge and sound as true, I don’t get a badge or sound when I receive the push on my device. I checked my Settings to ensure notifications are enabled for my app, and I also made sure my sound is turned on for my device.
Here’s my code:
angular.module('starter', ['ionic','ionic.service.core', 'starter.controllers', 'starter.services', 'ionic.service.push'])
.run(function($ionicPlatform, $ionicPush, $state) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
$ionicPush.init({
"debug": true,
"onNotification": function(notification) {
console.log(notification.text);
$state.go('tab.chat-detail', {chatId: '1'});
},
"onRegister": function(data) {
alert(data.token);
},
"pluginConfig": {
"ios": {
badge: "true",
sound: "true"
}
}
});
$ionicPush.register();
});
})
Here’s an example curl request:
curl -u my-private-key: -H "Content-Type: application/json" -H "X-Ionic-Application-Id: my-app-id"
https://push.ionic.io/api/v1/push -d '{"tokens": ["my-device-token"],"production": false, "notification":{ "alert":"Hi this is a
push notif", "title": "Push title", "android": {"payload": {"sound": "true","badge": "1","content-available": "1"}}, "ios":
{"payload": {"sound": "true","badge": "1","content-available": "1"}}}}'
Should be noted that with this particular request I was testing adding content-available to trigger the push behavior from the background, although I’m not sure if this option belongs in the payload or something different as the phonegap-plugin-push specifies that it’s part of the “aps” object. Either way, this setting should not be required to get badges and sound to work if the device is active/not asleep (I believe, anyway).
I notice in the doc for Push Usage that in onNotification there is a payload var, which is logged. However, in an iOS app, I wouldn’t log the payload. Do I need to do anything with this payload var to make it trigger on the device? I can’t imagine why this would be necessary, but just want to cover my bases.
Code example from Push Usage:
var push = new Ionic.Push({
"debug": true,
"onNotification": function(notification) {
var payload = notification.payload;
console.log(notification, payload);
},
"onRegister": function(data) {
console.log(data.token);
}
});
Does anybody have any thoughts on how to get badges and sound to work?