NativeScript / push-plugin

Contains the source code for the Push Plugin.
Apache License 2.0
123 stars 45 forks source link

Notifications can't show in my app #231

Open abinaj opened 6 years ago

abinaj commented 6 years ago

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:

lini commented 6 years ago

If you are already using the nativescript-plugin-firebase then you don't need to use the nativescript-push-notifications plugin. Cloud messaging is availble in nativescript-plugin-firebase out of the box and having the two plugins in the same app can lead to unexpected results.

cdsaenz commented 6 years ago

I am using the notifications plugin only and I'm having the same issue as abinaj. Probably is lack of understanding about the way this should work. I was also able to obten a token succesfully and then I try to send the test from postman and I get a success in Postman's console but I see nothing in the NS app (where I got the token). Am I missing something?

lini commented 6 years ago

Could you try using the firebase console (https://console.firebase.google.com/) first and see if the notifications are received from there? To send a notification from the console, open your project, select Cloud Messaging from the Grow menu, then select Send your first message or New Message. This will send a normal push that will show up in Android's notification drawer if the app is not already open. If it is open, then you will see the message in the app log.

cdsaenz commented 6 years ago

Yeah I had tried that lini but I got "invalid registration token, check the token format". I've suspended this part of the app for now but as soon as I resume will start from scratch.