设备未收到 Ionic3 Firebase 通知
Ionic3 Firebase notification not received on device
简而言之: 通过 Firebase Cloud Functions 发送的 Firebase 通知显示消息已发送。但是,设备未收到消息。 (仅在 Android 测试过。不知道 iOS)
您好,我在使用 Firebase Cloud Firestore、Cloud Functions 和其他 Firebase 服务的 Ionic 3 项目。
应用程序工作流程:
创建新文档后(如在新预订中),admin 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
云函数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 计划,但经常在日志中通知 - 未配置结算帐户。外部网络无法访问且配额受到严格限制。配置结算帐户以删除这些限制。
将 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);
});
});
简而言之: 通过 Firebase Cloud Functions 发送的 Firebase 通知显示消息已发送。但是,设备未收到消息。 (仅在 Android 测试过。不知道 iOS)
您好,我在使用 Firebase Cloud Firestore、Cloud Functions 和其他 Firebase 服务的 Ionic 3 项目。
应用程序工作流程:
创建新文档后(如在新预订中),admin 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
云函数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 计划,但经常在日志中通知 - 未配置结算帐户。外部网络无法访问且配额受到严格限制。配置结算帐户以删除这些限制。
将 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);
});
});