NativeScript FCM - txgz999/Mobile GitHub Wiki

FCM in Nativescript
  1. register the Android package name in Firebase console (find the package name from the nativescript/id field in package.json)
  2. download google-services.json and save it to the App_Resources/Android folder
  3. run tns plugin add nativescript-plugin-firebase
  4. add to src/app/app.component.ts:
    import * as firebase from "nativescript-plugin-firebase";
    
    ngOnInit(): void {
        firebase.init({
            onPushTokenReceivedCallback: function (token) {
                console.log("Firebase push token: " + token);
            },
            onMessageReceivedCallback: function (message) {
                console.log("Title: " + message.title);
                console.log("Body: " + message.body);
                // if your server passed a custom property called 'foo', then do this:
                console.log("Value of 'foo': " + message.data.foo);
            }
        }).then(
            instance => {
                console.log("firebase.init done");
            },
            error => {
                console.log(`firebase.init error: ${error}`);
            }
        );
    }
    
  5. To find the registration token at any time, use code like the following, which can be bound to a button tap event:
    onTap(args: EventData) {
        let button = args.object as Button;
        firebase.getCurrentPushToken().then((token: string) => {
            // may be null if not known yet
            console.log(`Current push token: ${token}`);
        });
    }