我正在我的Ionic 6应用程序中实现推送通知。我正在使用@capacitor/push-notifications
插件来管理我的离子应用程序中的推送通知。
import { Injectable } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { PushNotifications } from '@capacitor/push-notifications';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root',
})
export class FcmService {
constructor(private router: Router) {}
initPush() {
const fcmtoken = localStorage.getItem('fcmtoken');
if (Capacitor.platform !== 'web' && !fcmtoken) {
this.registerNotifications();
}
}
registerNotifications = async () => {
let permStatus = await PushNotifications.checkPermissions();
if (permStatus.receive === 'prompt') {
permStatus = await PushNotifications.requestPermissions();
}
if (permStatus.receive !== 'granted') {
throw new Error('User denied permissions!');
}
await PushNotifications.register();
await PushNotifications.addListener('registration', token => {
console.info('Registration token: ', token.value);
localStorage.setItem('fcmtoken', token.value);
});
await PushNotifications.addListener('registrationError', err => {
console.error('Registration error: ', err.error);
});
await PushNotifications.addListener('pushNotificationReceived', notification => {
console.log('Push notification received: ', notification);
});
await PushNotifications.addListener('pushNotificationActionPerformed', notification => {
alert(JSON.stringify(notification));
console.log('Push notification action performed', notification.actionId, notification.inputValue);
});
}
getDeliveredNotifications = async () => {
const notificationList = await PushNotifications.getDeliveredNotifications();
console.log('delivered notifications', notificationList);
}
}
我正在从AppComponent调用initPush()
,并且在我的应用中收到通知。但是当我点击那个通知时,什么也没有发生。pushNotificationActionPerformed
事件未被触发。我错过了一些配置。我在Android应用程序中使用它。
请帮助,如果有人已经实现了。
1条答案
按热度按时间0dxa2lsx1#
我在Android中也遇到了类似的问题,能够接收推送通知,但当我们点击接收到的通知时什么也没有发生。
我通过在AndroidManifest.xml中添加下面一行解决了这个问题
请在活动标签中添加以上行