Lor*_*ino 11 android ionic-framework ionic-native capacitor
大家早上好,我已经几个小时没能找到解决方案了。
我使用“PushNotifications”电容器插件(https://capacitor.ionicframework.com/docs/apis/push-notifications/)来收听来自firebase的推送通知(通知和数据类型),监听通知发生得非常好,即使在应用程序被杀死或在后台的情况下,一切都按预期运行。
问题如下:
我想在收到通知时打开该应用程序,如果它在后台或已被杀死。
在应用程序处于前台时收到通知的情况下,我可以使用自定义代码运行,
addListener(eventName: "pushNotificationReceived", callback)
并且在任何情况下我都没有问题,因为应用程序是打开的。
如果应用程序在后台时收到通知,我可以强制应用程序保持 backgroundMode 处于活动状态(https://ionicframework.com/docs/native/background-mode)并在收到通知后将应用程序带到前台通知。(虽然我不是很喜欢它,因为它很耗电)
在 app被杀的情况下,我还没有找到问题的解决方案。
似乎没有办法挂钩自定义代码,以便在后台收到推送通知或应用程序被杀死时能够在推送通知到达时运行,您是否遇到过这个问题?
谢谢!
我的解决方案是添加:FCM_PLUGIN_ACTIVITY操作到AndroidManifest.xml
<activity
...
android:name="com.appname.MainActivity"
...>
...
...
...
<intent-filter>
<action android:name="FCM_PLUGIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
此外,在服务器上,您应该在推送通知对象中包含该操作:
const pushNotification = {
data: {
...
},
notification: {
body: body,
title: title,
click_action: 'FCM_PLUGIN_ACTIVITY',
},
};
Run Code Online (Sandbox Code Playgroud)
在 ionic 项目中,您可以像下面一样处理pushNotificationActionPerformed并导航到所需的路线:
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
console.log('Push action performed: ' + JSON.stringify(notification));
}
);
Run Code Online (Sandbox Code Playgroud)
要在应用程序打开时处理接收推送通知,您应该处理pushNotificationReceived并在需要时自行显示 toast。
// the app is open, show your own notification if needed
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotification) => {
console.log('push notification received: ' + JSON.stringify(notification));
}
);
Run Code Online (Sandbox Code Playgroud)
小智 0
我遇到了同样的问题,并通过 AndroidManifest.xml 中的以下行修复了它
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="yourapp_notifications_channel_name" />
<service android:name="com.getcapacitor.CapacitorFirebaseMessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
根据需要更改“yourapp_notifications_channel_name”的名称,但您必须将其添加到电容器 PushNotification 中,如下所示:
if (this.platform.is('android')) {
const channel: Channel = {
id: 'yourapp_notifications_channel',
name: 'yourapp_notifications_channel',
sound: 'yourapp_notification_sound_file_name',
importance: 5,
visibility: 1,
vibration: true
};
await PushNotifications.createChannel(channel);
}
PushNotifications.addListener('registration', async (fcmToken: Token) => {
/// your notificaiton logic
}```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3656 次 |
| 最近记录: |