3

I am trying to add action buttons to the push notifications sent via the firebase admin SDK to my Ionic 4 app using the Firebase-X native plugin to handle push notifications. My app is running on android and ios.

Here's my current script that sends me successfully a push notification:

exports.sendDebugPush = functions.pubsub.schedule('* * * * *').onRun((context) => {
    let promises: Promise<any>[] = [];
    return admin.database().ref('/users/******').once("value")
    .then( user => {
        let todos = [];
        for(let key in user.val().nextActions) {
            if(user.val().nextActions[key].active != false) {
                let todo = user.val().nextActions[key]
                todo['todoid'] = key;
                todos.push(todo);
            }
        }
        if(todos.length > 0) {
            //currently we pick a random todo, later on the one with the highest priority
            //todos.sort((a, b) => (a.priority/1 < b.priority/1) ? 1 : -1);
            let randomTodo = todos[Math.floor(Math.random()*todos.length)]      
            let payload: any = {
                notification: {
                    title: "Gossik",
                    body: "Hoiiiii " + new Date().toISOString()
                },
                data: {
                    title: "Gossik",
                    body: "Hoiiiii " + new Date().toISOString(),
                    target: 'todo',
                    todoid: randomTodo.todoid
                }
            };
            Object.values(user.val().devices).forEach( (device) => {
                promises.push(admin.messaging().sendToDevice(String(device), payload));
            });
        }
        return Promise.all(promises)
        .then( () => {
            console.log('success!');
        })
        .catch( error => {
            console.log('failed :(');
            console.log(error);
        });
    });
});

Of course, without action buttons. And this function handles the push notifications in my app (this.firebase = FirebaseX plugin imported from 'import { FirebaseX } from "@ionic-native/firebase-x/ngx";'):

initPushNotifications() {
        this.firebase.getToken().then(token => {
            this.db.saveDeviceToken(this.auth.userid, token);
        });
        this.firebase.onMessageReceived().subscribe(data => {
            if(!data.target) {
                let title = '';
                if(data.title) {
                    title = data.title;
                } else if(data.notification && data.notification.title) {
                    title = data.notification.title;
                } else if(data.aps && data.aps.alert && data.aps.alert.title) {
                    title = data.aps.alert.title;
                }
                let body = '';
                if(data.body){
                    body = data.body;
                } else if(data.notification && data.notification.body){
                    body = data.notification.body;
                } else if(data.aps && data.aps.alert && data.aps.alert.body){
                    body = data.aps.alert.body;
                }
                this.alertCtrl.create({
                    message: title + ' ' + body,
                    buttons: [
                                {
                                    text: "Ok"
                                }
                            ]
                }).then( alert => {
                    alert.present();
                });
            } else {
                this.goToToDoPage(data.todoid);
            }
        });
    }

It does this also successfully. I achieved to handle the click on the push notification such that it redirects to my To-Do page for this kind of push notification (one with a 'target' property). But now I'd like to add two action buttons 'Start' and 'Skip' on the push notification to start or skip the corresponding to-do. To be clear, I am talking about a background push notification, so the app is not open. The user then gets a standard push notification on his phone and there I'd like two action buttons to take an action without opening the app itself.

I tried various things with the payload to first even show me action buttons, but didn't achieve it. For example, the following is not working for me:

let payload: any = {
                notification: {
                    title: "Gossik",
                    body: "Hoiiiii " + new Date().toISOString()
                },
                data: {
                    title: "Gossik",
                    body: "Hoiiiii " + new Date().toISOString(),
                    target: 'todo',
                    todoid: randomTodo.todoid,
                    "actions": [
                        { "icon": "approve_icon", "title": "APPROVE", "callback": "AppComponent.approve", "foreground": true},
                        { "icon": "reject_icon", "title": "REJECT", "callback": "AppComponent.reject", "foreground": true}
                    ]
                }
            };

Thanks a lot in advance for your help and let me know if something is still unclear. :)

0 Answers0