设备上未收到Ionic3 Firebase通知

时间:2018-03-01 15:21:18

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

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

您好,我正在使用Firebase Cloud Firestore,云功能和其他Firebase服务的Ionic 3项目。

应用工作流程

创建新文档后(如新预订中所示),管理员SDK应将推送通知发送到应该到达设备的特定设备。

问题:

签入云功能日志时,显示消息已成功发送,触发功能完成且没有任何错误。但尚未收到任何消息。但是,从Firebase Notification Console发送邮件时,每条邮件都会完美到达。

代码:

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)

...
// 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云功能日志显示:

enter image description here

离子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

云函数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计划是免费的,但通常会在日志中通知 - 结算帐户未配置。外部网络无法访问,配额严重受限。配置结算帐户以删除这些限制

1 个答案:

答案 0 :(得分:1)

将云功能中的功能修改为以下内容,现在,当应用处于后台时,通知托盘中会收到通知,当应用位于前台时,会在订阅响应中收到通知。

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);
            });
    });