应用程序处于前台时的IOS通知

时间:2018-01-05 17:46:27

标签: ios cordova ionic2 localnotification cordova-plugin-fcm

我正在使用cordova-plugin-fcm进行推送通知,以防后台和应用程序被杀死。如果应用程序在前台,我正在使用插件安排本地通知。这是我的代码:

            FCMPlugin.onNotification((msg) => {
            this.notificationMsg = msg;
            if (msg.wasTapped) {
             // code for app background and app killed
            }
            else {
                LocalNotifications.schedule({
                        id: 1,
                        title: 'title',
                        text: 'text',
                    })
           }
        })

但前台没有出现本地通知。但是一旦我最小化应用程序,它就会出现。这是什么问题?与插件版本有什么关系?请帮忙。

3 个答案:

答案 0 :(得分:0)

NotificationEventAdditionalData.foreground返回一个布尔值,如果应用程序位于前台,则为true,否则为false。

这个例子很适合我

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { TabsPage } from '../pages/tabs/tabs';

import { FCM } from '@ionic-native/fcm';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = TabsPage;
  isIOS: boolean = false;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private fcm: FCM) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.isIOS = platform.is('ios');

      if (this.isIOS) {
        // disable status bar overlay webview
        statusBar.overlaysWebView(false);
      } else {
        // let status bar overlay webview
        statusBar.overlaysWebView(true);
      }

      // set status bar to white
      statusBar.backgroundColorByHexString('#8c0f1a');

      // use light content
      statusBar.styleLightContent();

      splashScreen.hide();

    fcm.subscribeToTopic('marketing').catch(e => console.log('Error subscribing to topic', e));



        fcm.getToken().then(token => {
          // backend.registerToken(token);
          console.log(token);
        });

        fcm.onNotification().subscribe(data => {
          if (data.wasTapped) {
            console.log("Received in background");
          } else {
            console.log("Received in foreground");
          };
        });

        fcm.onTokenRefresh().subscribe(token => {
          // backend.registerToken(token);
          console.log(token);
        });
      }

  }
}

答案 1 :(得分:0)

使用以下cordova本地通知插件,这对我来说非常有用: https://github.com/katzer/cordova-plugin-local-notifications

该插件创建了对象cordova.plugins.notification.local,并且可以在触发deviceready后访问。

示例:

cordova.plugins.notification.local.schedule({
title: 'My first notification',
text: 'Thats pretty easy...',
foreground: true
});

答案 2 :(得分:0)

如果您想使用FCMPlugin在前台接收通知,则只需编辑以下文件

https://github.com/fechanique/cordova-plugin-fcm/blob/master/src/android/MyFirebaseMessagingService.java#L53

更改以下行:

//sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(), remoteMessage.getData());

至:

sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), data);
remove and add the platform you using and you are all set