App is run forever

Hi ,

i am using ionic 2 with pushnotification.

used this plugin ionic plugin add phonegap-plugin-push

It’s work only on app is running.

I need app is running forever like whatsup.

i also used cordova background mode.

Still not work

how can i able to do this.

can u please help me.

Thanks & regards

1 Like

Then you didn’t implement correctly. Push notifications work always, also when the app is not running or in background.

Thanks for ur response.May be my code is incorrect.Kindly give me idea for app is keep running forever.

phonegap-plugin-push will never receive push noti. if your app is closed.
Try using OneSignal plugin.
It will receive notification even though your app is closed.

Thanks for ur reply.I have one doubt.can i used onesignalplugin for FCM/GCM pushnotification?
and also what is the googleProjectNumber

you need to make account on onesignal website. and there you will have to provide your GCM server key and sender ID.

something like this.
image

Thank u.Is Notification receive also app not running ?

Yeah… i am using it and it’s receiving notification even though app is closed.

@ravi_sojitra Thanks for ur patient.I have account for onesignal website also i created a new app.

How can i get Google project Number?

Google project number means sender id from your firebase account.

i am getting this err ERROR Error: Uncaught (in promise): plugin_not_installed.But i installed the plugin using this

ionic cordova plugin add onesignal-cordova-plugin

Can you show me your code where you are using that plugin?
Did you wrap that code inside platform.ready() function?

Here is my code

import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import { OneSignal } from ‘@ionic-native/onesignal’;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {

constructor(private oneSignal: OneSignal,public navCtrl: NavController) {
this.oneSignal.startInit(‘5d4fd62b-b9f8-4e41-9a89-7ea3b18af3de’,‘470761651487’);

this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);

this.oneSignal.handleNotificationReceived().subscribe((d) => {
	// do something when notification is received
	console.log("NOTIFICATION RECEIVED");
	console.log(d);
});

this.oneSignal.handleNotificationOpened().subscribe((d1) => {
  	// do something when a notification is opened
  	console.log("NOTIFICATOIN OPENED");
  	console.log(d1);

});

this.oneSignal.endInit();

}

}

I also import app module…

Wrap your code inside platform.ready() function.


import { NavController,Platform } from 'ionic-angular'; //import platform
@Component({
   selector: 'page-home',
   templateUrl: 'home.html'
})
export class HomePage {

    constructor(private oneSignal: OneSignal,public navCtrl: NavController,public platform:Platform) { // add platform here
        platform.ready().then(()=>{ //add this function

            this.oneSignal.startInit('5d4fd62b-b9f8-4e41-9a89-7ea3b18af3de','470761651487');

            this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);

            this.oneSignal.handleNotificationReceived().subscribe((d) => {
                // do something when notification is received
                console.log("NOTIFICATION RECEIVED");
                console.log(d);
            });

            this.oneSignal.handleNotificationOpened().subscribe((d1) => {
                // do something when a notification is opened
                console.log("NOTIFICATOIN OPENED");
                console.log(d1);

            });

            this.oneSignal.endInit();
        });
    }

}

Ya.It’s wrk.How can i send notification?

In onesignal dashboard you will find New Message in left side menu.
Fille up the details and send notification from there.

@ravi_sojitra. i try to send notifications.I got this err msg

My device cannot receive notifications.Can u explain briefly

Did you open an app because it needs to be opened once to register your app to onesignal.
If you did then
add this function after this.oneSignal.startInit('5d4fd62b-b9f8-4e41-9a89-7ea3b18af3de','470761651487');
line

this.oneSignal.getIds().then((ids)=>{
        alert(ids.userId);
      }).catch((e)=>{
        alert(e);
      })

and let me know what did you get.

Yeah.I got ids.userID.