receive notification when app is closed using ionic
Asked Answered
F

2

6

I am using ionic2 with FCM.

I receive notification when app is running.

I need to receive notification when app is running or not.

How can i do this ?

Forked answered 21/6, 2017 at 7:6 Comment(1)
please elaborate more, tell us how are you sending the notification? and the code you are executing.Flanigan
S
0

You can use the One-Signal which solves your need : here is bit of code how to initialize in the ionic-2

Install the OneSignal Cordova plugin via Terminal

ionic cordova plugin add onesignal-cordova-plugin
 npm install --save @ionic-native/onesignal

Important Note : make sure you import the provider into your app.module.ts as instructed on Ionic Native site, e.g.

import {OneSignal} from '@ionic-native/onesignal';
@NgModule({
  ...
  providers: [
    ...
    OneSignal
    ...
  ]

})
export class AppModule { }

Taken from the : Ionic-native

Now Init the OneSignal plugin.

Then, add the following code to your app.ts so that it runs on startup, e.g inside the initializeApp() method:

import {OneSignal} from '@ionic-native/onesignal';
import {Platform} from 'ionic-angular';

constructor(private _OneSignal: OneSignal, private _platform: Platform) {
  startApp();
}

  startApp() {
    this._platform.ready().then(() => {
      this._OneSignal.startInit(appId, googleProjectId);
      this._OneSignal.inFocusDisplaying(this._OneSignal.OSInFocusDisplayOption.Notification);
      this._OneSignal.setSubscription(true);
      this._OneSignal.handleNotificationReceived().subscribe(() => {
        // handle received here how you wish.
      });
      this._OneSignal.handleNotificationOpened().subscribe(() => {
        // handle opened here how you wish.
      });
      this._OneSignal.endInit();        
    })    
  }

Note: substitute in your:

appId from OneSignal

googleProjectId from FCM

Swingle answered 12/9, 2017 at 6:29 Comment(0)
L
0

If you are already receiving the notification when the application is open, I assume you already have a server to send it to (as with the Firebase Cloud Functions) and a plugin that you have configured to receive in your application.

First, case the problem is on IOS, verify if the generated project (after ionic cordova build) have the REMOTE PUSH NOTIFICATIONS enabled in XCode. In Android this is enabled by default.

Another thing that can be the problem is the properties you are sending in your payload AND the plugin your are using to receive it. For a notification show up in the plugin cordova-plugin-firebase it have to have a notification property with title or body, like this:

{ "notification": { "title": 'a title', "body": 'some text' }, "data": { "anydata1": 'data1', "anydata2": 'data2', "etc": 'etc' } }

With the plugin phonegap-plugin-push the rules are similar. You have to have the notification property. It all depends on the plugin.

Longoria answered 14/9, 2017 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.