Navigate to route after push notification is received in main.ts?


#1

Can somebody help me navigate after push notification is received??
I get “cannot read property ‘navigate’ of undefined”

This code is inside main.ts and there is no class or constructor to initialize variables:


import { RouterExtensions } from "nativescript-angular/router";

let router: RouterExtensions;
setTimeout(() => {

    let firebase = require("nativescript-plugin-firebase");  

    firebase.init({ 
        onPushTokenReceivedCallback: function(token) {             
            deviceToken = token;  
            console.log("Firebase push token: " + token);  
        },  
        onMessageReceivedCallback: (message) => {  
           // let router: RouterExtensions; //tried initializing it here but didnt work also
            dialogs.alert({  
                title: "Push message: " + (message.title !== undefined ? message.title : ""),  
                message: JSON.stringify(message.website),  
                okButtonText: "OK"  
            });
            router.navigate(["webViewPage"],{
           clearHistory:true,
           transition: {
            name: "slideTop",
            duration: 800,
            curve: "spring"
            },
            queryParams:{"url": "http://www.google.com"}, 
            animated: true
      
          });
            
        }
..........

Also tried this.router.navigate… but it didnt work


#2

try adding the navigation into the app launched event :slight_smile:

import * as app from "application"
 onMessageReceivedCallback: (message) => {  
           // let router: RouterExtensions; //tried initializing it here but didnt work also
            dialogs.alert({  
                title: "Push message: " + (message.title !== undefined ? message.title : ""),  
                message: JSON.stringify(message.website),  
                okButtonText: "OK"  
            });



app.on("launch",()=>{
 router.navigate(["webViewPage"],{
           clearHistory:true,
           transition: {
            name: "slideTop",
            duration: 800,
            curve: "spring"
            },
            queryParams:{"url": "http://www.google.com"}, 
            animated: true
      
          });
}); 

app.on("resume",()=>{
 router.navigate(["webViewPage"],{
           clearHistory:true,
           transition: {
            name: "slideTop",
            duration: 800,
            curve: "spring"
            },
            queryParams:{"url": "http://www.google.com"}, 
            animated: true
      
          });
}); 
        }

and on the resume event


#3

Thank you :smiley: I resolved it by adding firebase.init to app.component.ts and using constructor to initialize router. I saw no difference but if i have any error i will move to this solution :wink:


#4

I use this for some local notifications :slight_smile: