Challenges arise when updating routes after resuming the application using AngularJS and the Cordova push notifications plugin

Having trouble rerouting when the app is resumed; I'm not sure why it's not working. The resume alert pops up and a topic id appears, but $location.path() doesn't seem to be functioning as expected. Any ideas?

To provide more insight: upon receiving the notification, the lastMessage is stored. When the app is resumed, relevant data is retrieved, however, $location.path() doesn't behave as intended. Interestingly, the same call to $location.path does work in other parts of the code.

In the onResume function within the notification factory:

function onResumeListener() {
document.addEventListener('resume', function() {
    alert("resuming");
  var lastMessage;

  try {
    lastMessage = JSON.parse($window.localStorage.getItem('lastMessage'));
      alert(lastMessage.topic_id);
      //$location.path("/chat/" + lastMessage.topic_id);
      //tried putting route change here as well

  } catch (exception) {
      alert("failure");
    lastMessage = '';
  }

    $location.path("/chat/" + lastMessage.topic_id);

});
}

When receiving apple notifications:

function onNotificationAPN(e) {
$window.localStorage.setItem('lastMessage', JSON.stringify(e));
$rootScope.$broadcast('push.message', e);

if (e.alert) {}
if (e.sound) {}
if (e.badge) {}
}

Upon receiving android notifications:

function onNotificationGCM(e) {
$window.localStorage.setItem('lastMessage', JSON.stringify(e));

switch (e.event) {
  case 'registered':
    localStorage.setItem('deviceId', e.regid);
    $rootScope.$broadcast('push.register', e.regid);
    break;
  case 'message':
    //alert('broadcast');
    $rootScope.$broadcast('push.message', e);
    break;
  case 'error':
    $log.error('Error event received: ' + e);
    break;
  default:
    $log.error('Unknown event received: ' + e);
    break;
}
}

Answer №1

After encountering a similar issue, I managed to find a solution by implementing a slight workaround. Instead of directly changing the location, I wrapped the action in a timeout function:

$timeout(function(){$location.path('/newPath')},0);

The reason behind why this works is still unclear to me. My hypothesis is that at the moment of clicking the notification, Angular might not have fully loaded yet. By delaying the location change using a timeout of 0, it gives Angular the opportunity to catch up. It's also possible that the additional apply method triggered by $timeout plays a role in resolving the issue...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

SheetJS excel-cell customization

I'm using this example to export a worksheet from https://github.com/SheetJS/js-xlsx/issues/817. How can I apply cell styling such as background color, font size, and adjusting the width of cells to fit the data perfectly? I have looked through the do ...

Emphasizing terms with angular edges

I am struggling to implement a feature where users can click on a word within a specific div and have it highlighted. Despite my efforts, I realize that what I have created is far from functional. I have watched numerous tutorials and read various articles ...

Animation glitches occurred following the update to Android Jelly Bean, appearing as duplicated animations on

I am currently working on developing an HTML5 app for Android using JQuery 1.1.0 and Phonegap 1.9.0. The app includes a small animation of a battery being drawn on the canvas and updating dynamically. This animation functioned perfectly on Android 4.0.4. ...

Error message appears when attempting to submit a form with empty values in NodeJs

I need help troubleshooting a display error that occurs when setting empty values in form fields. When I delete the row with res.redirect('/') and then attempt to register, if I refresh the page later, an error appears. However, if I keep this re ...

Parsing JSON sub items in Android application using Java

Here is a snippet of my PHP file: <?php $myObj = array( "name"=>"John" , "age"=>"30" , "post"=>[ "title"=>"What is WordPress" , "excerpt"=>"WordPress is a popular blogging platform" , ...

Leveraging the `instanceof` operator within AngularJS expressions

Can I utilize the "typeof" method in AngularJS? I am using ngRepeat to iterate through my data and need to determine whether each item is a string or an object. <tr ng-repeat="text in data"> <td>{{angular.isObject(text) && 'IsObject& ...

Issue with Bootstrap Nav Dropdown in Yeoman Gulp-Angular configuration

I recently built an Angular App using the Swiip/generator-gulp-angular framework and ran into some issues when trying to implement a bootstrap nav dropdown menu. After searching for a solution, I came across this helpful Stack Overflow thread: Bootstrap D ...

Leveraging thousands of on() event listeners in Firebase demonstrates exceptional design

We are looking to perform operations on our Firebase database and modify data based on user input from their mobile devices by changing a flag. Currently, we are using the `on()` method to listen to specific flags within each user's node. This listen ...

Is it possible to rewrite this function recursively for a more polished outcome?

The function match assigns a true or false value to an attribute (collapsed) based on the value of a string: function match(children) { var data = $scope.treeData for (var i = 0; i < data.length; i++) { var s = data[i] for (var ...

Anticipating outcome: row 1 column 1 (character 0) in Python

As a newcomer to the world of Python, I am currently attempting to parse data in my application using the following lines of code: json_str = request.body.decode('utf-8') py_str = json.loads(json_str) Unfortunately, when I reach this line (jso ...

How do you call the breeze from Angular, whether it be in a service or controller? Can

I am currently working on an angular project where I am creating a form that retrieves data from a stored procedure using $http get in an angular service. My goal is to databind some dropdown lists in the filter of this html form. The data is stored in a v ...

The model name should not be overridden if it is already being used in different fields

I have a model that I am binding on two sides. The first side is inside an html p tag, and the second side is a textarea. When I make changes to the textarea, the content inside the p tag also changes. How can I ensure that only the content in the textar ...

Storing data seamlessly within a controller using AngularJS on the same page

Can someone please assist me with saving data into an array and displaying it in a table within a form? <div ng-app="farmerapp" ng-controller="RegController"> @Html.TextBoxFor(x => x._Cetificate.CertificateName,new { data_ng_model = "_Ce ...

Using $index in ng-class to dynamically apply form validation styles

Looking to validate an angular form input and add a class if it's not $valid. The catch is, it's nested inside an ng-repeat looping through an array of integers, with the input name based on the $index: <li ng-repeat="item in selectedItem.dat ...

The perplexing problem of scrolling in Dojox/mobile/scrollableview

I've noticed some scroll issues with the dojox/mobile/ScrollableView (version 1.10+) in my cordova application, specifically on Android phones. This problem seems to be affecting other widget-based architectures and even some store apps as well. I wou ...

The Json array does not adhere to the correct format

Trying to structure a JSON array in the following format [ "1-35e03ac0b6ba442bb0d5fab3faef32fe", { "request": [ "UpdateComplianceDetail", { "remarks": "Remarks", "next_due_date": "27-Mar-2016", "completion_date" ...

Display the superscript notation for the header of a UI-Grid cell

The scope variable here holds a value of m3. Is there a way to display 3 as superscript next to m in the UI grid column header? $scope.Unit = m3; ...

Implementing a Class Addition Functionality Upon Button Click in AngularJS

I have a form that initially has disabled fields. Users can only view the information unless they click the edit button, which will enable the fields with a new style (such as a green border). I want to add a class to these fields that I can customize in m ...

Learn the process of extracting a particular value from JSON data and displaying it in HTML by utilizing AngularJS's ng-repeat directive

As a newcomer to angularjs, I am encountering difficulties retrieving and displaying a specific value from a json file in order to showcase it on my view page using angularjs ng-repeat for image source. My goal is to repeat the json file based on a particu ...

Error encountered during build: The specified resource value for <color> is invalid

Whenever I attempt to launch my Flutter App on a Pixel 5 Device, I encounter this error message: /Users/username/.gradle/caches/transforms-3/0ace7b4637c402760ef38d3581a65ee0/transformed/appcompat-1.4.2/res/values/values.xml:37:4: Invalid <color> for ...