Skip to content
This repository was archived by the owner on Feb 7, 2019. It is now read-only.
This repository was archived by the owner on Feb 7, 2019. It is now read-only.

Notifications can't show in my app #231

Open
@abinaj

Description

@abinaj

I tried this for android, but I can't push notifications in application.

My code application,

in main-view-model.ts

import { Observable } from "tns-core-modules/data/observable";
import * as pushPlugin from "nativescript-push-notifications";
export class HelloWorldModel extends Observable {
 private pushSettings = {
        // Android settings
        senderID: "387819224537", // Android: Required setting with the sender/project number
        notificationCallbackAndroid: (stringifiedData: String, fcmNotification: any) => {
            const notificationBody = fcmNotification && fcmNotification.getBody();
            console.log(notificationBody)
            this.updateMessage("Message received!\n" + notificationBody + "\n" + stringifiedData);
        }
         // iOS settings
        // badge: true, // Enable setting badge through Push Notification
        // sound: true, // Enable playing a sound
        // alert: true, // Enable creating a alert
        // notificationCallbackIOS: (message: any) => {
        //     this.updateMessage("Message received!\n" + JSON.stringify(message));
        // }
    };

    private _counter: number;
    private _message: string;

    constructor() {
        super();
        this.message = "";
        this.updateMessage("App started.");

        let self = this;
        this.onRegisterButtonTap();
    }

    get message(): string {
        return this._message;
    }

    set message(value: string) {
        if (this._message !== value) {
            this._message = value;
            this.notifyPropertyChange("message", value);
        }
    }

    onCheckButtonTap() {
        let self = this;
        pushPlugin.areNotificationsEnabled((areEnabled: Boolean) => {
            self.updateMessage("Are Notifications enabled: " + !!areEnabled);
        });
    }

    onRegisterButtonTap() {
        let self = this;
        pushPlugin.register(this.pushSettings, (token: String) => {
            self.updateMessage("Device registered. Access token: " + token);
            // token displayed in console for easier copying and debugging durng development
            console.log("Device registered. Access token: " + token);

            if (pushPlugin.registerUserNotificationSettings) {
                pushPlugin.registerUserNotificationSettings(() => {
                    self.updateMessage("Successfully registered for interactive push.");
                }, (err) => {
                    self.updateMessage("Error registering for interactive push: " + JSON.stringify(err));
                });
            }
        }, (errorMessage: String) => {
            self.updateMessage(JSON.stringify(errorMessage));
        });
    }

    onUnregisterButtonTap() {
        let self = this;
        pushPlugin.unregister(
            (successMessage: String) => {
                self.updateMessage(successMessage);
            },
            (errorMessage: String) => {
                self.updateMessage(JSON.stringify(errorMessage));
            },
            this.pushSettings
        );
    }

    private updateMessage(text: String) {
        this.message += text + "\n";
    }

}

in app.ts

import * as application from 'application';
import firebase = require("nativescript-plugin-firebase");

firebase.init({
}).then(
    (instance) => {
        console.log("Fierbase init done!");
    },
    (error) => {
        console.log("Firebase init error: " + error);
    }
);

application.run({ moduleName: 'app-root' });

in main-page.xml

<StackLayout class="p-20">
	<Button text="Are notifications enabled?" tap="{{ onCheckButtonTap }}" android:visibility="collapsed" class="btn" />
	<Button text="Register device" tap="{{ onRegisterButtonTap }}" class="btn" />
	<Button text="Unregister device" tap="{{ onUnregisterButtonTap }}" class="btn" />
	<Label text="Log:" class="h1" />
	<ScrollView orientation="vertical">
		<Label text="{{ message }}" class="h2" textWrap="true" />
	</ScrollView>
</StackLayout>

in mobile show this image

When I tried to post from Postman in my app doesn't show any notification. Please can you ask me, who is the problem?

in Postman I tried like in image:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions