我将firebase插件与Ionic 4应用程序配合使用。当用户单击前台推送时,我有一个用例来实现。即他必须重定向到其他用户界面。我怎样才能做到这一点?我可以使用背景推送点击的用例来做到这一点。
fcmListeners() {
this.firebase.onNotificationOpen().subscribe(async (data: any) => {
if (data.tap) {// when user tapped the background notification
this.pushRedirection(data);
console.log("Received in background-data", data);
} else {
console.log("Received in foreground", data);
this.pushRedirection(data);
}
});
}
如果我使用上述类型的代码,则无需用户单击即可自动转到所需的页面。但是,如果用户仅单击推送,则需要实现它。那你能告诉我如何处理吗?
有效载荷:
{
"registration_ids": [
"dlaNHzi7Gy8:APA91bGdn_jXFJm1xdGGI44s48JbXEu5iYa"
],
"notification": {
"body": "Push Test Facility Category",
"title": null,
"icon": "myicon",
"sound": "mySound",
"vibrate": 1,
"Badge": 1,
"click_action": "FCM_PLUGIN_ACTIVITY"
},
"data": {
"message": "Push Test Facility Category",
"title": null,
"b": "22",
"id": "2",
"category_name": "Restaurants",
"h": "1",
"p": 1,
"type": "2"
}
}
this.firebase.onNotificationOpen().subscribe(async (data: any)
注意:我可以在此处为Android实现自定义通知吐司,因为它不会在前台通知中显示任何内容。但是iOS默认情况下会显示前台通知。那么如何处理呢?
答案 0 :(得分:1)
Firebase Corodva插件当前显示推送通知,即使收到的通知是前台应用程序,也是如此,但它是 an issue ,并且不应是默认行为。如您所见,在合并此修复程序之前,您可以像这样在本地修复该问题:
我改变了
PROJECT_ROOT/plugins/cordova-plugin-firebase/src/ios/AppDelegate+FirebasePlugin.m
而且
PROJECT_ROOT/platforms/ios/PROJECT_NAME/plugins/cordova-plugin-firebase/AppDelegate+FirebasePlugin.m
在第183行的两个文件中,我都替换了
completionHandler(UNNotificationPresentationOptionAlert);
使用
completionHandler(UNNotificationPresentationOptionNone);
现在,它的行为与预期的一样(前台没有通知,仅在 背景)
解决此问题后,您可以使用data.tap
并且应该可以正常工作:
if (data.tap) {
// when user tapped the background notification
this.pushRedirection(data);
console.log("Received in background-data", data);
}
如果您需要在CI / CD环境中执行此操作,或者只想使其自动化,则可以创建 Cordova Hook 。
为此,请首先在cordova-hooks
中创建一个新的src
文件夹,然后再创建一个名为fix_push-notifications-foreground_issue.js
的新js文件,如下所示:
src
|- ...
|- cordova-hooks
|- fix_push-notifications-foreground_issue.js
|- node_modules
|- ...
请注意,还有其他一些方法可以添加Cordova钩子(例如使用默认的hooks
文件夹),但是我还是更喜欢使用自定义文件夹。可以 here 找到更多信息。
在新的js文件中粘贴以下代码:
#!/usr/bin/env node
module.exports = function (context) {
const fs = require('fs');
const path = require('path');
const issue = `completionHandler(UNNotificationPresentationOptionAlert);`;
const fix = `completionHandler(UNNotificationPresentationOptionNone);`;
// ISSUE: iOS: Push notifications are shown when in foreground
// https://github.com/arnesson/cordova-plugin-firebase/issues/817
function start() {
console.log('+------------------------------------+');
console.log('| Starting PN Foreground Fix Hook |');
console.log('+------------------------------------+');
const pluginDir = path.join(context.opts.projectRoot, '/plugins/cordova-plugin-firebase-lib/src/ios');
const pluginFile = path.join(pluginDir, 'AppDelegate+FirebasePlugin.m');
const platformPluginDir = path.join(context.opts.projectRoot, '/platforms/ios/Bilbayt/Plugins/cordova-plugin-firebase-lib');
const platformPluginFile = path.join(platformPluginDir, 'AppDelegate+FirebasePlugin.m');
applyFixToFile(pluginFile);
applyFixToFile(platformPluginFile);
console.log('+------------------------------------+');
}
/**
* Fixes the push notification foreground issue on the file sent as parametner
* @param {*} file Path of the file to be fixed
*/
function applyFixToFile(file) {
if (fs.existsSync(file)) {
let content = fs.readFileSync(file).toString('utf8');
if (content.indexOf(issue) > -1) {
content = content.replace(issue, fix);
fs.writeFileSync(file, content);
console.log(` PN foregorund fix applied to ${file} file`);
} else {
console.log(` No need to apply PN foreground fix to ${file} file`);
}
} else {
console.log(` Unable to find ${file} file`);
}
}
// Start the hook
start();
}
最后,将钩子添加到您的config.xml
文件中:
<widget ...
<hook src="cordova-hooks/fix_push-notifications-foreground_issue.js" type="before_prepare" />
<platform name="android">
...
</platform>
<platform name="ios">
...
</platform>
</widget>
现在,每次构建应用程序时,都应自动应用此修复程序:)