简体   繁体   中英

Ionic Local Notifications trigger on status change

I am using the ionic Local Notifications Plugin for my Table Booking ionic App. I have a web dashboard that controls the Ionic App.

When a booking is approved in the dashboard and the status changes I am trying to get a local notification to trigger.

I have the notification working great however I have a major issue I cannot figure out.

In the ionic app a user sees a list of all their bookings, then when they select the booking it opens a page with all the booking details including a visual reference to their booking status, ie Pending, Approved or Cancelled.

here is my component:

export class OrderDetailsPage {

    orderId: any;
    orderDetails: any = {
        status: ''
    };
    booking: any = {};





    constructor(private localNotifications: LocalNotifications, public modalCtrl: ModalController, private calendar: Calendar, public af: AngularFireDatabase, private device: Device, private appAvailability: AppAvailability, private platform: Platform, public navCtrl: NavController, public navParams: NavParams) {

        this.orderId = this.navParams.get('id');
        this.af.object('/orders/' + this.orderId).subscribe(res => {
            this.orderDetails = res;

            //Call my check status function to trigger notification
            this.checkStatus();

            var orderId = res.orderId;
            this.af.list('/bookings', {
                query: {
                    orderByChild: 'orderId',
                    equalTo: orderId
                }
            }).subscribe(res => {
                this.booking = res[0];
            });

        })



    }



    checkStatus(){

      if (this.orderDetails.status == 'Accepted') {
              this.localNotifications.schedule({
                        id: 1,
                        title: 'Your Booking Was Accepted!',
                        text: 'Your booking has been Accepted by the restaurant!',
                        sound: ''
                    });
            }
    }
}

The orderId is passed from the order list page, this helps me get the particular order to show on the order details page.

The issue I am facing is the notification is only triggering when i literally go into the order detail page which is pointless.

I want to detect globally, when a particular order status changes for that particular user and that particular booking, from Pending (which is set by default in firebase) to Approved and not when a user goes into the order details page.

I thought about trying the code in the root of the app, but I wont be able to get the id from the navParams to assign to the orderId variable.

I am not even sure what I want to do is even possible with local notifications?

For that use case, you have to implement remote notifications. You will need to add a hook on your server, so when the order changes, you also send a notification using a service like node-apns or Ionic Push. Local Notifications can not be modified in the background with Ionic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM