我在Ionic 4应用程序中使用了firebase插件。当用户单击前台推送时,我有一个用例来实现。即他必须重定向到其他用户界面。我怎样才能做到这一点?我可以使用背景推送点击的用例来做到这一点。
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默认情况下会显示前台通知。那么如何处理呢?
最佳答案
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/YourAppName/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>
现在,每次构建应用程序时,都应自动应用此修复程序:)