NgCordova / Ionic Push Notifications when the app is in the background

I am currently creating an Android application using ionic / ngcordova. I am going to implement push notifications. I implemented push notifications as a service that is introduced at the app.run(function(){..}) stage. The registration part works, and I get a callback containing regid . In addition, when the application is in an active state, an event is raised and a notification is received.

The problem I am facing is that when the application goes into the background, notifications are not accepted at all. I would expect a local notification to be raised when the application is not running or something like that, but absolutely nothing happens, which is strange.

I tried the Internet over the past few days, looking for a solution, but I could not find anything that tells me that it should just work.

Below is my application notificationService.js inside my application

 app.factory('notificationService', ['$cordovaPush', function($cordovaPush){ var dataFactory = {}; // // When the device is ready and this service has been plumbed in... document.addEventListener("deviceready", function(){ console.log("initializing push notifications..."); _register(); }, false); // // Registers the device for push notifications... var _register = function(){ var config = {}; if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){ // TODO: centralise this value as it can change... config = { senderID: "448168747432", ecb: "onNotificationGCM" }; }else { // iOS config = { "badge":"true", "sound":"true", "alert":"true" }; // Can add the following property to the config object to raise a callback with the information if need be... // "ecb": "onNotificationRegisterAPN" } $cordovaPush.register(config).then(function(result){ // // Typically returns "ok" for android and devicetoken for iOS console.log(result); }); }; window.onNotificationGCM = function(result){ console.log(result); /* I get called when the app is in the foreground, but nothing happens when the app is in the background. */ }; dataFactory.register = _register; return dataFactory; }]); 

If this helps, I use PushSharp through the .net app to deliver notifications. Any help would be greatly appreciated.

UPDATE: I use the following / libs frameworks:

  • Ionic Framework 1.2.14-beta6
  • Cordoba 4.2.0
  • Pushplugin
+6
source share
1 answer

For everyone who pulled their hair in a couple of days, like me, the solution was very simple ... I lost two properties in my QueshNotification Pushsharp request. So using the example provided in the github PushSharp repository here: https://github.com/Redth/PushSharp#sample-code

 push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE-REGISTRATION-ID-HERE").WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}")); 

You must update to add the missing properties:

 push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE") .WithJson(@"{""alert"":""This is the future"",""badge"":7,""sound"":""sound.caf"",""title"":""Status Bar title"",""message"":""Some text you want to display to the user""}")); 

Otherwise, if your application is developed using Cordoba, and it is not in the foreground, nothing, repeat nothing will happen.

Connect my hat to gdelavald with his comment on PushPlugin to point me in the right direction:

https://github.com/phonegap-build/PushPlugin/issues/212

+3
source

Source: https://habr.com/ru/post/982354/


All Articles