在我的角13 Ionic 6应用程序中,我使用了电容/推送通知和FireBase,在我的服务中:
import { EventEmitter, Injectable, Output } from '@angular/core';
import {ActionPerformed, PushNotifications, PushNotificationSchema, Token} from '@capacitor/push-notifications';
import { Capacitor } from '@capacitor/core';
import { Dialogs, DialogType } from '../Utilities/dialogs';
import { get, set, remove } from './storage.service';
import { BehaviorSubject } from 'rxjs';
import { App } from '@capacitor/app';
//https://devdactic.com/push-notifications-ionic-capacitor/
@Injectable({
providedIn: 'root'
})
export class FbNotificationsService {
onFCMTokenChanged: BehaviorSubject<string> = new BehaviorSubject('');
tokenProcessed: boolean = false;
constructor(private dialogs: Dialogs) {
}
initPush() {
if (Capacitor.getPlatform() !== 'web') {
this.registerPush()
} else {
console.log('Web platform detected');
this.onFCMTokenChanged.next('web-platform');
}
}
private registerPush() {
PushNotifications.requestPermissions().then((permission) => {
if (permission.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
console.log('FB Permission granted');
PushNotifications.register(); //Registers app to receive notifications
} else {
// No permission for push granted by user on iOS
console.log('FB NO permissions granted');
}
});
PushNotifications.addListener(
'registration',
(token: Token) => {
if ((token)&&(!this.tokenProcessed)) {
this.tokenProcessed = true;
console.log('My token: ' + JSON.stringify(token));
this.onFCMTokenChanged.next(token.value);
set('device-token', token.value);
}
});
PushNotifications.addListener('registrationError', (error: any) => {
console.log('Registration Error: ' + JSON.stringify(error));
this.dialogs.dialogClose('Network Error','The network signal is not sufficient at this time. Please improve connection and try again');
App.exitApp();
});
PushNotifications.addListener(
'pushNotificationReceived',
async (notification: PushNotificationSchema) => {
console.log('Push received: ' + JSON.stringify(notification));
alert('Push received: ' + JSON.stringify(notification));
//What to do next. Notification received on the device.
}
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
async (notification: ActionPerformed) => {
const data = notification.notification.data;
console.log('Action performed: ' + JSON.stringify(notification.notification));
alert('Push action performed: ' + JSON.stringify(notification));
//What to do next. data holds all payload fields of the notification object
}
);
}
}我遵循的基本指南是这里。
当我从Firebase控制台发送测试消息时,我放置的警报语句将弹出,这意味着应用程序接收到消息:
alert('Push received: ' + JSON.stringify(notification)); //WORKING但是,操作系统没有弹出通知消息供用户查看。我总是收到来自Whatapp、电子邮件等的通知,“不打扰”设置被关闭。
这是有经验的两款安卓设备:小米Rednote 8和三星Galaxy A32。
我是不是遗漏了什么?
发布于 2022-07-26 08:39:38
我以前也遇到过类似的问题,也许下面的信息可以帮助你:
当你的安卓应用程序在前台时,PushNotifications不会出现在屏幕上。
如果您绝对希望看到通知的显示,则可以在收到推送通知时创建本地通知。
https://stackoverflow.com/questions/72849760
复制相似问题