【问题标题】:Ionic3 Firebase notification not received on device设备上未收到 Ionic3 Firebase 通知
【发布时间】:2018-08-09 17:04:24
【问题描述】:

简而言之:通过 Firebase Cloud Functions 发送的 Firebase 通知显示消息已发送。但是,设备中没有收到消息。 (仅在Android中测试。不了解iOS)

您好,我在使用 Firebase Cloud Firestore、Cloud Functions 和其他 Firebase 服务的 Ionic 3 项目。

应用工作流程:

在创建新文档时(如在新预订中),管理 SDK 应向应到达设备的特定设备发送推送通知。

问题:

查看 Cloud Functions 日志时,显示消息已成功发送,并且触发功能已完成且没有任何错误。但是还没有收到任何消息。但是,当从 Firebase 通知控制台发送消息时,每条消息都会完美到达。

代码:

index.ts(云函数)

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

exports.notifyOnNewBooking = functions.firestore
    .document('users/{userId}/bookings/{bookingId}')
    .onCreate( event => {
        const bookingData = event.data.data();

        // construct notification message here
        const message = {
            notification: {
                title: 'Reservation confirmed!',
                body: 'Your reservation at someplace at sometime is confirmed',
                icon: 'https://image.ibb.co/iBwekx/icon.png'
            }
        };

        // send notification with message right away
        return admin.messaging().sendToDevice(bookingData.deviceFCMToken, message, {priority: 'high'})
            .then(resp => {
                console.log("sent successfully", resp);
            })
            .catch(err => {
                console.error("could not send notification", err);
            });
    });

app.component.ts(离子)

...
// Ionic Native wrapper
import { FCM } from '@ionic-native/fcm';
....

@Component({
  template: `
  ....
  ....
`
})
export class MyApp {
  ...
  constructor(..., private fcm: FCM) {}

  ngOnInit() {
    this.fcm.onNotification()
      .subscribe(resp => {});
  }
}

Firebase Cloud Functions 日志显示:

Ionic CLI 信息

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

System:

    Node : v9.3.0
    npm  : 5.5.1 
    OS   : macOS High Sierra

Misc:

    backend : pro

Cloud Functions package.json 依赖项

  "dependencies": {
    "@google-cloud/functions-emulator": "^1.0.0-beta.3",
    "firebase-admin": "~5.8.1",
    "firebase-functions": "^0.8.1",
    "firebase-tools": "^3.17.4",
    "global": "^4.3.2"
  },

config.xml

<plugin name="cordova-plugin-fcm" spec="^2.1.2">
  <variable name="SENDER_ID" value="845539284400" />
</plugin>

注意:只有一个订阅位于应用程序的根组件中。而且我正在使用 Firebase Spark 计划,该计划是免费的,但经常在日志中通知 - 计费帐户未配置。外部网络无法访问,配额受到严格限制。配置结算帐户以删除这些限制

【问题讨论】:

  • 嘿哟,我认为您在 FCM 插件初始化中缺少重要的令牌注册步骤:fcm.getToken().then(token=>{ backend.registerToken(token); })
  • 基本上在启用通知之前,您需要在本地获取令牌,然后将其传递给您的“后端”,以便服务器端逻辑可以发送通知
  • @SergeyRudenko - 不,我没有错过。我在其他地方注册了令牌,因为我的应用程序需要这样做。我最近尝试从 Cloud Functions 发送数据通知,并且可以完美接收。但对于正常通知,应用程序不会收到它。另一方面,仅在onNotification() 的订阅中收到数据通知消息,而不是在我想要的系统通知托盘中。我不知道这是预期的行为还是什么。
  • 抱歉,您能否澄清一下您的应用在您的场景中是在前台运行还是在后台运行?基本上,如果您的应用程序位于 FOREGROUND - 您的应用程序只能获取通知对象中由“数据”obj 指定的有效负载,并且不会有基于托盘的通知。但是,当您的应用程序选项卡或在后台时 - 您将从“通知” obj 而不是从数据中获取数据 - 有意义吗?

标签: firebase firebase-cloud-messaging ionic3 google-cloud-functions ionic-native


【解决方案1】:

将 Cloud Functions 中的函数修改为以下内容,现在当应用程序处于后台时,通知托盘中会收到通知,而当应用程序处于前台时,则在订阅响应中收到通知。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

exports.notifyOnNewBooking = functions.firestore
    .document('users/{userId}/bookings/{bookingId}')
    .onCreate(event => {
        const bookingData = event.data.data();

        // construct notification message here
        const message: admin.messaging.Message = {
            token: bookingData.deviceFCMToken,
            android: {
                notification: {
                    title: 'Reservation successful',
                    body: `Your reservation at ${bookingData.restaurant_name} is confirmed.`,
                    icon: 'https://image.ibb.co/iBwekx/icon.png'
                }
            },
            apns: {
                headers: {
                    'apns-priority': '10'
                },
                payload: {
                    aps: {
                        alert: {
                            title: 'Reservation successful',
                            body: `Your reservation at ${bookingData.restaurant_name} is confirmed.`,
                        },
                        badge: 1
                    }
                }
            }
        };

        // send notification with message right away
        return admin.messaging().send(message)
            .then(resp => {
                console.log("sent successfully", resp);
            })
            .catch(err => {
                console.error("could not send notification", err);
            });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多